Total Pageviews

Wednesday, April 22, 2009

Text Box Enter Key in ASP.NET

One of the common requests in ASP.NET is to submit a form when visitor hits an Enter key. That could 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 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 tag of your page. The javascript code should 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 could 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.

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 except you placed one invisible textbox on web form. :)

Maybe it is not best practice, but placing invisible textbox could 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 many text boxes and many 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.

Enter Key in ASP.NET


One of the common requests in ASP.NET is to submit a form when visitor hits an Enter key. That could 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 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 tag of your page. The javascript code should 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 could 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.


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 except you placed one invisible textbox on web form. :)

Maybe it is not best practice, but placing invisible textbox could 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 many text boxes and many 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 could 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 many text boxes and buttons as you want.


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
or 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.

----

Or We can use the Panel/Form DefaultButton Property to make the button to default press when we press the enter key..

For Panel Control in asp.net


<asp:Panel ID="Panel1" runat="server" DefaultButton="">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:button ID="btnDefault" runat="server" Text="Default Button Panel" OnClientClick="btnDefault_Click" />
</asp:Panel>



For Form tag in asp.net

<form id="form1" runat="server" defaultbutton="btnDefault">   
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:button ID="btnDefault" runat="server" Text="Default Button Form" OnClientClick="btnDefault_Click" />
</form>






After making the default button, press enter key on textbox you will notice that without pressing Button click events, btnDefault's btnDefault_Click is fired.



---
Enjoy Programming



5 comments:

  1. Taken much time to elaborate the concept. This elaborative tutorial (I would like to call this article a tutorial, a knowledgebase one)helps to understand the concept of default button in asp.net page that is useful for both beginners and mid-level programmers. Thanks for such a good post!

    Hoping more on asp.net in future! Happy Programming!

    ReplyDelete
  2. Hi Yogesh. do you know the way to get this enter key event in web user control.i've created search user control that has one textbox and link button. when i use this control in other form, i want to capture enter key event. Please help..I need it urgently..Thanks..

    ReplyDelete
  3. Hi dear...
    I have already explain this scenario here..
    but can u tell me more about your problem so that i can help u

    ReplyDelete
  4. I think u need to capture search textbox enter click event to fire the linkbutton Click events...
    so u can use asp:panel (if u are asp.net developer) or u can use javascript which is listed here to achieve your needs

    ReplyDelete

Blog Archive

Ideal SQL Query For Handling Error & Transcation in MS SQL

BEGIN TRY BEGIN TRAN --put queries here COMMIT; END TRY BEGIN CATCH IF @@TRANCOUNT>0 BEGIN SELECT @@ERROR,ERRO...