Default Focus, Buttons and Validation Errors with ASP.NET 2.0
One of the common requests with ASP.NET 1.1 today is to have better control over what happens when a user in a browser hits the enter key without having a button or post-back control selected.
ASP.NET 2.0 makes this easier by supporting the concept of a "default button" that can be indicated on either a <form> or <asp:panel> container control. If no button is selected at the time the enter key is selected, the defaultbutton property will drive the appropriate post-back to the server and route the message to the control you want.
<html>
<body>
<form defaultfocus=“textbox2” defaultbutton=“button1” runat=“server”>
<asp:textbox id=“textbox1” runat=“server”/>
<asp:textbox id=“textbox2” runat=“server”/>
<asp:button id=“button1” text=“Same Page” runat=“server”/>
<asp:panel defaultbutton=“button2” runat=“server”>
<asp:textbox id=“foo” runat=“server”/>
<asp:button id=“button2” runat=“server”/>
</asp:panel>
</form>
</body>
</html>
· Here if the enter key is selected the first time the page is loaded, "button1" will be the button that receives the post-back event.
· If the enter key is hit while the user has their cursor focus within the "foo" textbox (contained wtihin the <asp:panel>), then "button2" will be the button that receives the post-back event.
There is a catch in this feature, when the page is using Master Page. In this case, below would be required to handle the situation.
protected void Page_Load(object sender, EventArgs e)
{
Page.Form.DefaultButton = Button2.UniqueID;
}
protected void Page_PreRender(object sender, EventArgs e)
{
Page.SetFocus(TextBox2);
}
Thanks & Regards,
Arun Manglick || Tech Lead