ASP.NET 2.0 - Enter Key - Default Submit Button

One of the most annoying things in developing web pages is handling the "Enter key" for form submission. Enter key has been the favourite way users like to submit forms. Though we provide Buttons to click on, the easiest and intuitive way is that, I can enter some text, make some changes and then hit "Enter" to accomplish my submission.

"Enter" Key is handled in a little tricky way by uplevel browsers like Internet Explorer, when it comes to ASP.NET.

  • If there is a single Textbox and single button, then it becomes straight forward, the button is submitted. However, the event code doesnt get executed, though the page postsback.

  • If there are two or more, buttons, then it takes up the first button as the default button. However, it still doesnt execute the event handler but just refreshes the page.

You can supress the Enter key event using Javascript. But this would result in other undesirable effects like, any Enter key in the form i.e. within Text Area or basically where large text is entered, would be disabled.

The earlier work around was to associate a javascript function to each Button to verify the that the relevant button is submitted upon Enter key.

ASP.NET 2.0 introduces a wonderful work around for this. By simply specifying the "defaultbutton" property to the ID of the <asp:Button>, whose event you want to fire, your job is done.

The defaultbutton property can be specified at the Form level in the form tag as well as at panel level in the <asp:panel> definition tag. The form level setting is overridden when specified at the panel level, for those controls that are inside the panel.

Also, the Event Handler for the specified button, fires thereby simulating a true submit button functionality.

The following sample code contains a form and 4 panels with each of them containing different buttons. It can be noticed that for each panel, there is a default button specified which would trigger the corresponding button's event handler when "Enter" Key is pressed upon a text changed event.



<form id="form1" runat="server" defaultbutton="btn1">

<div>

<asp:TextBox ID="txt" runat="server"></asp:TextBox>

<asp:Button ID="Button5" runat="server" Text="Cancel" OnClick="Button5_Click" />

<asp:Button ID="btn1" runat="server" Text="Submit" OnClick="btn1_Click" />

<asp:Panel ID="pnl1" runat="server" defaultbutton="Button1">

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

<asp:Button ID="Button1" runat="server" Text="Button1" OnClick="Button1_Click" />

</asp:Panel>

<asp:Panel ID="Panel1" runat="server" defaultbutton="Button2">

<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>

<asp:Button ID="Button2" runat="server" Text="Button2" OnClick="Button2_Click" />

</asp:Panel>


<asp:Panel ID="Panel2" runat="server" defaultbutton="Button3">


<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>

<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>

<asp:Button ID="Button3" runat="server" Text="Button3" OnClick="Button3_Click" />

</asp:Panel>

<asp:Panel ID="Panel3" runat="server" defaultbutton="Button4">

<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>

<asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>

<asp:Button ID="Button4" runat="server" Text="Button4" OnClick="Button4_Click" />

</asp:Panel>

</div>

</form>

The corresponding, sample events for the button clicks are


protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(Button1.Text);
}


protected void Button2_Click(object sender, EventArgs e)
{
Response.Write(Button2.Text);
}


protected void Button3_Click(object sender, EventArgs e)
{
Response.Write(Button3.Text);
}


protected void Button4_Click(object sender, EventArgs e)
{
Response.Write(Button4.Text);
}


protected void btn1_Click(object sender, EventArgs e)
{
Response.Write(btn1.Text);
}


protected void Button5_Click(object sender, EventArgs e)
{
Response.Write(Button5.Text);
}

Once we execute the above functionality, we can notice, the corresponding Buttons' text are displayed when the Enter key is pressed from within a panel and at the form level, it fires the btn1 Button's event.

This would be a very useful feature in scenarios where we have different sections of the page and would like to have Enter key fire corresponding submit button events.

Cheers and Happy Enter Keying !!!

posted @ Wednesday, April 12, 2006 1:22 PM

Print

Comments on this entry:

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Nick at 2/26/2007 1:57 PM
Gravatar
Nice one - thank you very much!

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Brian at 4/2/2007 6:17 PM
Gravatar
Works for me. Thanks for the info.

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Sabarinth at 4/26/2007 5:23 AM
Gravatar
Not working in firefox.

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Adeel at 8/27/2007 10:48 PM
Gravatar
great... :)

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Jaco at 12/17/2007 2:10 AM
Gravatar
What to do when the input field and button are located within a user control?

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Dukebaby at 2/1/2008 6:43 AM
Gravatar
It works in FireFox for me.

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Ben at 2/4/2008 9:22 AM
Gravatar
I second Jaco's question: What if the button is located within a user control?

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by rao at 3/17/2008 11:32 PM
Gravatar
thank you very much

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Jagwio at 3/25/2008 7:30 AM
Gravatar
Thanks a lot... exactly what I was looking for! (And works fine in Firefox)

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Ben at 4/25/2008 1:23 AM
Gravatar
Just what we were looking for as well, for solving the issue in IE. Thanks! (And as other visitors noted, the issue didn't even need to be solved in Firefox! ;)

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Gary at 5/13/2008 2:40 AM
Gravatar
And how to do it in Content page when using master pages?

I tried programmatically setting Form.DefaultButton = "btnOK" in Page_Load but that causes an error.

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by chuck at 5/14/2008 12:05 AM
Gravatar
Use the UniqueID property of the button to set the Form.DefaultButton. Example: Form.DefaultButton = btnOK.UniqueID.

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by b2b at 9/9/2008 1:33 AM
Gravatar
great, it works!

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by fdhgd at 9/13/2008 10:16 AM
Gravatar

dfhd




# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Abhishek at 10/4/2008 2:36 AM
Gravatar
this was a great help...

Thanks..

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by <div> at 10/6/2008 2:03 PM
Gravatar


# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by nicoleta at 10/7/2008 11:25 PM
Gravatar
when you go with tab to the cancel button and hit enter, instead of calling the Button5_Click (to display cancel)still executes the btn1_click (displaying submit)

any idea how to fix this?

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Ales at 10/15/2008 12:17 PM
Gravatar
Great! You saved us lot of time. Thank you!

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by yty at 10/20/2008 2:14 PM
Gravatar
hggght
gfgfg


















# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Umesh at 10/24/2008 1:41 PM
Gravatar
Very nice solution. g8888...keep it up...

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by WotLK Power Leveling at 11/2/2008 10:45 PM
Gravatar
You save me a whole day, dude. Thanks

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by LastEnd.com at 11/10/2008 8:19 AM
Gravatar
My site and users thank you. Before I was splitting the form.

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Chris Williams at 11/21/2008 11:23 PM
Gravatar
this just saved my butt today. thanks man!

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Srikantha at 11/26/2008 2:53 PM
Gravatar
Thank you so much for the easy solution. It really helped me.

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Lochner at 12/2/2008 12:33 PM
Gravatar
Excellent! Thanks!

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Jitendra Kumar at 12/8/2008 9:50 AM
Gravatar
Thanks you for giving a useful information.

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Krishna at 1/3/2009 1:55 PM
Gravatar
Can this be possible with HTML submit control? I used HTML Control. How can i acheive the same..? Plz help me out

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Makarand Yadav at 1/7/2009 2:39 PM
Gravatar
Nice, thanks a lot!

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Sai Krishna at 1/9/2009 12:52 PM
Gravatar
Nice article.. :) It helped me a lot.. Thanks

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by arvind singh kushwaha at 1/27/2009 3:35 PM
Gravatar

the solution is very nice but i work only first enter press

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Andreas Beraz at 1/27/2009 9:25 PM
Gravatar
what is with DefaulButton & MultiLine & Firefox?

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Rahul at 1/27/2009 10:39 PM
Gravatar
This was great, helped me run a fix on a site very easily.

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by JimmyT at 3/11/2009 12:41 AM
Gravatar
Thanks, this saved me lots of time today!

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by vira at 3/17/2009 2:44 PM
Gravatar
it will not work if master page and content page both having button and press enter in each text box.

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by club penguin at 3/17/2009 5:55 PM
Gravatar
Thank you so much for the easy solution. It really helped me.

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Huma at 3/24/2009 1:48 AM
Gravatar
Thank You for the solution.

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Ben at 4/1/2009 2:44 AM
Gravatar
On the content page , you can wrap your control in a panel ,so you can specify the defaultButton for the panel.

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Mahdy at 4/29/2009 3:15 PM
Gravatar
Thx alot 4 ur help

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Mashrur at 7/1/2009 3:07 AM
Gravatar
Hi, if there is a calendar control inside panel, and if that was modified and pressed enter - default button does not work :(

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Naveen at 7/3/2009 11:46 AM
Gravatar
Good one

# ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Eeshwar at 7/30/2009 12:51 AM
Gravatar
Dear Frnds,

I have to seen ur code, but i have similar to same problem , i my page 6 buttons and i am using asp buttons ans div tags..ok,
now we need when i enter key then submit want to fire and when ever i go by tab order that cooresponding button, then i put form load "defaultbutton="<buttonid>" " but its not working properly...


so plz help me any one

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by celston at 7/30/2009 2:51 PM
Gravatar
I think I was able to address the default button being inside a user control by doing the identical code for the code-behind/Page_Load of the user control itself. It seems to work but I'm not sure what would happen if you have more than one control set this value.

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Dinesh at 8/15/2009 1:11 AM
Gravatar
Thanks..Pls Keep it up..it saved my time and a day..gr8..

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Jose at 8/17/2009 4:09 PM
Gravatar
Thank you for this SIMPLE solution. I was starting to use the __doPostback() and crazy JS blocks...yuck. So simple and clean. Thumbs UP UP!

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by javaChip.AJAX at 9/2/2009 10:27 PM
Gravatar
thanks for the solution..btw, anyone know a solution if i want the Nnter key to act like the Tab key? TIA!

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by chan at 9/11/2009 12:56 AM
Gravatar
//search is button id
//added to protected void Page_Load

Page.ClientScript.RegisterHiddenField("__EVENTTARGET", search.UniqueID);

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by David at 10/1/2009 7:43 AM
Gravatar
Thanks! I had my form in a table with the textbox and button inside a td so I had to put the asp:panel inside the td to get it to work.

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by Shane at 10/4/2009 10:23 PM
Gravatar
"uplevel browsers like Internet Explorer"

Isn't that a contradiction in terms?

:)

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by classic video games at 10/31/2009 2:42 PM
Gravatar
Thank you for this very SIMPLE solution. I was starting to get confused

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by auto insurance at 11/3/2009 4:12 AM
Gravatar
Use the UniqueID property of the button to set the Form.DefaultButton. Example: Form.DefaultButton = btnOK.UniqueID.

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by JSON at 11/6/2009 9:14 AM
Gravatar
This solution will NOT work if a control in the form does not have focus. If you have a drop down list and select a value and hit enter, this will not fire the form to post back because the ddl does not have focus. So it's very easy to implement, but limited in ways.

# re: ASP.NET 2.0 - Enter Key - Default Submit Button

Left by cleaner reviews at 11/16/2009 5:13 AM
Gravatar
Thanks for sharing. I don't like the uniqueID.

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345