Thursday, September 10, 2009

Default Button in ASP.Net Form

One can set the default button at the form level meaning of all the buttons in the form the one that is set as default will be triggered whenever user presses Enter key.
To set the default button one has to do the following
<form id="index" runat="server" defaultbutton="btn1">
<asp:Button ID="btn1" runat="server" Text="Button"
    OnClick = "btn1_Click" />
<asp:Button ID="btn2" runat="server" Text="Button"
    OnClick = "btn2_Click" />
<asp:Button ID="btn3" runat="server" Text="Button"
    OnClick = "btn3_Click" />
<asp:TextBox ID="txt1" runat="server">asp:TextBox>
form>
Need to provide the ID of the Button to the defaultbutton property and it is set as default.
If you want to do it from code behind refer below (C#)
this.Form.DefaultButton = "btn1"; 
 To do the same things in Master Form, use the below code in code behind
this.Form.DefaultButton = btn1.UniqueID;
  

No comments:

Post a Comment