ASP.NET Web Forms Panel and DefaultButton

Quick, short, and useful ASP.NET feature…

You usually expect that if a user presses Enter while inside a non-multiline TextBox would cause the form to be submitted via your Button on the bottom of the form.

However, if you have multiple “form areas” in your ASP.NET web form, one of the weird “isms” is that the user pressing Enter in one of the fields can have fairly random results, because the browser chooses which Submit button to use, usually based on the order it appears in the HTML. Some conditions will cause your Button_Click event to fire, some not.

A quick and simple way to solve this is to wrap your form section in an <asp:Panel> control, but use the DefaultButton attribute. So…

<asp:Panel runat=”server” ID=”myPanel” DefaultButton=”button1”>
<asp:Button runat=”server” ID=”button1” Text=”This is my button” />


… will cause button1 to be the default “enter key” button when the user is within that panel.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s