10,000 Monkeys - Harnessing the Power of Typing Monkeys

America's 2,672,401st Most Read Blog by Kevin Marshall.
in

Default Buttons on ASP.NET 2.0 Forms

First I want to welcome the new reader I gained from my Coding4Fun article.  Perhaps your dog accidentaly jumped on the keyboard and clicked the link to my site or maybe you're new to the english language and you were looking for the German Coding4Fun site.  Willkommen.  I'm sure you'll find a wealth of literary delights 10-15 minutes of mindless reading here.

Anyway, how do you set the default button when you have multiple buttons on a form?  My first response would be "Just build a winform app and stop trying to hack together desktop app like functionality on the web"  (Note: Several explatitves were removed since this is a family friendly site)  Now with ASP.NET you can set the default button on forms and panels so that if you click Enter in a text box, it'll submit with that button.

So if you have two different sets of inputs with save buttons, you can place them in a panel and set the default button for input fields in that panel.  Like so:

<html>
<body>
     <form runat=“server”>
          <asp:panel defaultbutton=“btn2” runat=“server”>
              <asp:textbox id=“Name” runat=“server”/>
              <asp:button id=btn2 runat=“server”/>
          </asp:panel>
          <asp:panel defaultbutton=“btn3” runat=“server”>
              <asp:textbox id=“Address” runat=“server”/>
              <asp:button id="btn3" runat=“server”/>
          </asp:panel>
     </form>
</body>
</html>

Now you can fire off different events all over your form with a simple click of the Enter key!
Posted: Mar 16 2006, 08:39 AM by kmarshall | with no comments
Filed under:

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required)