Default Buttons in ASP.net

by Phillip H. Blanton 30. April 2009 05:29

Those of you who grew up writing Windows software are familiar with the idea of a default button. ASP.net has the same idea, but did you know that you can have multiple default buttons on the same form? I know at first glance that doesn't seem to make sense. How can you have more than one "default" button?

I have seen many javascript hacks to get a default button to click when someone presses the enter key on a form...

Man, my writing skills are sucking out loud. Let's get right to it and not try to make it eloquent, as my eloquent bone seems broken today.

ASP.net provides a simple solution for handling the Default Button functionality differently for different areas of the form, by using an asp:Panel control. The Panel control has a property called "DefaultButton" that takes the ID of the button that you want clicked whenever a user presses the Enter key while inside a control that resides in the panel control's collection.

<asp:Panel runat="server" DefaultButton="btnProcess">   
    Name: <asp:TextBox id="tbName" runat"server" /><br/>     
    Address: <asp:TextBox id="tbAddress" runat"server" /><br/>   
    Phone: <asp:TextBox id="tbPhone" runat"server" /><br/>   
    <asp:Button id="btnProcess" runat="server" />
</asp:Panel>

When the user is inside the Name, Address or Phone control and presses Enter, the btnProcess button will be clicked. You may have as many of these Panel controls as you wish; each with their own default button. That makes it easy to provide the users with an intuitive interface via the web. It's just a matter of knowing how the controls interact with each other.

Tags:
Categories:

Add comment




  Country flag


  • Comment
  • Preview
Loading