An ASP.NET Blog
I work for Microsoft and help people and businesses make better use of technolgy to realize their full potential. The opinions mentioned herein are solely mine and do not reflect those of my employer.

ASP.NET 2.0 - Enter Key - Default Submit Button

Wednesday, April 12, 2006 1:22 PM

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 !!!


Feedback

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

Nice one - thank you very much! 2/26/2007 1:57 PM | Nick

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

Works for me. Thanks for the info. 4/2/2007 6:17 PM | Brian

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

Not working in firefox. 4/26/2007 5:23 AM | Sabarinth

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

great... :) 8/27/2007 10:48 PM | Adeel

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

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

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

It works in FireFox for me. 2/1/2008 6:43 AM | Dukebaby

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

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

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

thank you very much
3/17/2008 11:32 PM | rao

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

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

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

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! ;) 4/25/2008 1:23 AM | Ben

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

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. 5/13/2008 2:40 AM | Gary

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

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

Post a comment





 

Please add 7 and 2 and type the answer here: