Jonas Stawski

Everything .NET and More

Multiple Default Buttons

Today I learned something new about ASP.NET. I learned that you can have multiple default buttons based on where you are in the form. For example if you have a form with a Save button and other buttons to do some server side checking you can set the default button of the form to be the Save button and use the DefaultButton property of the Panel container to specify which button should be the default button for that specific part of the page.

<form id="form1" runat="server" defaultbutton="btnSave">
    <asp:TextBox id="tb" runat="server" />
    <asp:Panel id="pnlOne" runat="server" DefaultButton="btnAdd">
      <asp:TextBox id="tb1" runat="server" /><asp:Button id="btnAdd" runat="server" text="Add" />
    <asp:Panel id="pnlTwo" runat="server" DefaultButton="btnDelete">
      <asp:TextBox id="tb2" runat="server" /><asp:Button id="btnDelete" runat="server" text="Delete" />
    <asp:Button id="btnSave" runat="server" text="Save" />

So the code above has 3 default buttons. If you are standing in the “tb” TextBox and press enter the “btnSave” will be clicked. When you press enter while on the “tb1” TextBox the “btnAdd” will execute and finally if you are standing in the “tb2” TexBox the “btnDelete” will be clicked.

Everyday you learn something new is a good day.

Happy Programming!

Comments (3) -

this is really awesome but is there any other method for implenting this


This shit saved day ! Thank you


Thanq you sir


Add comment