|
 Thursday, March 29, 2007
« Overcomplicating simple things - ASP.NET... | Main | Handling unexpected exceptions and traci... »

ASP.NET HOWTO: Enable Default Enter Button in ASP.NET 2.0

In certain forms, such as search forms, users enter a value into the search query box, then press the enter button. If you are develolping in ASP.NET, the page will refresh, but nothing will happen. The buttons click event wont fire... UNLESS, you explicity enable a default enter button on the form.

A problem that existed in ASP.NET 1.x was that you could not specify a default enter button unless you provided some custom code. In ASP.NET 2.0 you can do this very easily, unfortunately its not that widely used, much less known.

HOW TO:

The HtmlForm object has a property DefaultButton. This property gets or sets the control that that causes the post back when the ENTER key is pressed.

Example

<body>
   <form id="form1" runat="server" defaultbutton="processRequestButton">
      <div>
         <asp:TextBox AccessKey="c" runat="server" ID="somevalueTextbox"></asp:TextBox>
         <asp:Button runat="server" ID="processRequestButton" Text="Process" OnClick="processRequestButton_Click" />
      </div>
   </form>
</body>

Result

When the user presses the ENTER key, the form will be posted back and the processRequestButton_Click event will be fired.

Simple!

kick it on DotNetKicks.com
#    Comments [4] |
Thursday, March 29, 2007 10:11:12 AM (Eastern Standard Time, UTC-05:00)
Event better.... if you need to specify that a particular "area" of your page has a default button, and another "area" of the page has a different default button, you can use a <asp:panel> control and set the default button on that control, directly.

<asp:panel runat="server" id="pnl1" defaultbutton="b1">
<asp:TextBox runat="server" id="txt1" />
<asp:button runat="server" id="b1" text="b1" />
</asp:panel>

<asp:panel runat="server" id="pnl2" defaultbutton="b2">
<asp:TextBox runat="server" id="txt2" />
<asp:button runat="server" id="b2" text="b2" />
</asp:panel>

that way you can have a "search" and a "login" and whatever other form sections on your screen and when you are in a particular textbox or panel area, that panel will only submit what you want, to the correct button click.
Thursday, March 29, 2007 10:38:25 AM (Eastern Standard Time, UTC-05:00)
VERY true.

I use the exact method you describe often. I use it when using the View or Wizard control a lot. :)

Wednesday, April 04, 2007 2:55:02 PM (Eastern Standard Time, UTC-05:00)
Would it be possible for you to provide an RSS feed to your site? Please forgive me if I'm just too damn blind. :)
Wednesday, April 04, 2007 3:00:23 PM (Eastern Standard Time, UTC-05:00)
Oops... thanks for letting me know. I must have hid it when I updated the interface! Oops!
Here it is until I fix it: http://feeds.feedburner.com/donnfelker

:) Thanks!
Name
E-mail
(will show your gravatar icon)
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview