Enter Key in ASP. NET-Complete Research

Source: Internet
Author: User
Tags sample html code classic asp

One of the common requests in ASP. NET is to submit a form when visitor hits an Enter key. that cocould be a case if, for example you want to make Login Screen. it is expected that user just hit enter when he insert a user name and password instead to of forcing him to use a mouse to click login button. if you want to make search function on your web site, it is frequently required to give a possibility to hit enter after you insert a search terms instead of mouse click on a Search button.

In HTML or classic ASP pages is not hard to submit forms using the enter key on keyboard. programmer use a <input type = "submit"> to make a default button. if web site visitor click on that button or press enter key, the form will be submited.

Of course, you can have a more than one form on your page and individual submit button for every form.

You don't want to submit a form with Enter key?

Rarely, you will need to disable an Enter key and avoid to submit form. if you want to prevent it completely, you need to use OnKeyDown handler on <body> tag of your page. the javascript code shocould be:

If (window. event. keyCode = 13)

{

Event. returnValue = false;

Event. cancel = true;

}
Common ASP. NET problems with Enter key

If you try to use Enter key in ASP. NET, according to your browser's type, you can get really weird results. for example, try to place one ASP. NET textbox and a button to the web form. write a code on a OnClick event of a button. that coshould be something simple, like:

Response. Write ("The button was clicked! ");

Now start debugging and write something to textbox. If you press enter while focus is on textbox, form will submit, but your code for button's click event will not be executed.

Stop the debuging and place one more simple HTML textbox to the form. you will not write anything in this textbox, so you can even make it invisible. just place it somewhere inside of your form tag.

<INPUT type = "text" style = "VISIBILITY: hidden; POSITION: absolute">

Start debugging again. you cannot see the second textbox, and everything looks like before. try again to write something in first textbox. if you press enter now, form will submit, and your code for button's click event will now be executed. this is extremely different behavior, and you did nothing should t you placed one invisible textbox on web form.

Maybe it is not best practice, but placing invisible textbox cocould be simple solution for you if you have only one button on your web form. But, what if you have a different situation? What if you have a few buttons with only one textbox, or more than one text box with only one button, or your text boxes and your buttons with different code for each button, and all that on one form?

Different browsers have a different behavior in these cases. in case that you have more buttons, only first button will be "clicked" every time. so, we need some other approach to get an universal solution.



How to make a default button in ASP. NET



We need to specify exactly which button will be "clicked" when visitor press Enter key, according to which textbox currently has a focus. the solution cocould be to add onkeydown attribute to textbox control with this code:

TextBox1. Attributes. add ("onkeydown", "if (event. which | event. keyCode) {if (event. which = 13) | (event. keyCode = 13) {document. getElementById ('"+Button1. UniqueID + "'). click (); return false ;}} else {return true };");

This line of code will cause that button Button1 will be "clicked" when visitors press Enter key and cursor is placed in TextBox1 textbox. on this way you can "connect" as plain text boxes and buttons as you want.

Easy solution for default button

There is a free component that allows you to assign a button to the "enter-pressed" client side event of input controls. if you type some text in textbox and press Enter, the form will postback, and the serverside click event of your button is fired. you don't need to write any code, but you only need to use this control's "DefaultButton" property. it you are a beginner programmer this cocould be a life saver. more about MetaBuilders DefaultButtons Control you can find at http://www.metabuilders.com/Tools/DefaultButtons.aspx



Default buttons in ASP. NET 2.0 and ASP. NET 3.5

ASP. NET 2.0 makes this problems easier and introduce a concept of a "default button ". new defaultbutton attribute can be used with <form> or <asp: panel> control. what button will be "clicked" depends of where actually cursor is and what button is chosen as a default button for form or a panel.

Here is sample HTML code that contains one form and one panel control:

<Form defaultbutton = "button1" runat = "server">

<Asp: textbox id = "textbox1" runat = "server"/>

<Asp: textbox id = "textbox2" runat = "server"/>

<Asp: button id = "button1" text = "Button1" runat = "server"/>


<Asp: panel defaultbutton = "button2" runat = "server">

<Asp: textbox id = "textbox3" runat = "server"/>

<Asp: button id = "button2" runat = "server"/>

</Asp: panel>


</Form>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.