In certain forms, such as search forms, users enter a value into the search query box, then press the enter button. If you are develolping in ASP.NET, the page will refresh, but nothing will happen. The buttons click event wont fire… UNLESS, you explicity enable a default enter button on the form.
A problem that existed in ASP.NET 1.x was that you could not specify a default enter button unless you provided some custom code. In ASP.NET 2.0 you can do this very easily, unfortunately its not that widely used, much less known.
HOW TO:
The HtmlForm object has a property DefaultButton. This property gets or sets the control that that causes the post back when the ENTER key is pressed.
Example
<body>
<form id=“form1” runat=“server” defaultbutton=“processRequestButton“>
<div>
<asp:TextBox AccessKey=“c” runat=“server” ID=“somevalueTextbox”></asp:TextBox>
<asp:Button runat=“server” ID=“processRequestButton“ Text=“Process” OnClick=“processRequestButton_Click” />
</div>
</form>
</body>
Result
When the user presses the ENTER key, the form will be posted back and the processRequestButton_Click event will be fired.
Simple!
Leave a Reply
You must be logged in to post a comment.