Pages

Sunday, September 11, 2011

Submit form on Enter Key Default submit Button

Whenever we want user to submit a form by filing out some textboxes on the aspx page, though we provide the button to submit the form but user prefer to press enter and expect the form to be submitted



But in ASP.NET when user press enter key the page gets post back but the button event doesn't get fired hence whatever code u've written in the click event doesn't get executed and it just like refreshing the page , to handle this situation in ASP.NET 2.0 ,we need to set the Default button property which indicates which button events should get fired when user press enter key to submit the form
We can set Default button property either in Form or if we've placed a panel than in panel:


<body>
<form id="form1" runat="server"
                 defaultbutton="Button1">
<div>
    <asp:TextBox ID="TextBox1" runat="server">
    </asp:TextBox><br />
    <br />
    <asp:TextBox ID="txtTest" runat="server">
    </asp:TextBox>
    <asp:Button ID="Button1" runat="server"
         OnClick="Button1_Click" Text="Button" />
</div>
</form>
</body>
or if we use panel than
<asp:Panel ID="Panel1" runat="server" defaultbutton="Button1">

No comments:

Post a Comment