Tuesday 4 December 2007

DefaultButton

When entering data into a web form, users often press the Enter key instead of clicking on a submit button. Classic HTML pages can have multiple forms, each having a separate submit button. This means that when a control gets focus in the first form, the page will automatically trigger the respective event if the user presses the Enter key.

ASP.NET pages can only contain one server-side Form tag where all server-side controls must be defined. This only becomes a problem when a page requires several form sections. For example, a page might include two form sections; search and log in. If these both have their own submit buttons and are defined in the same server-side Form tag, how does the page know which event should be triggered when the user presses the Enter key? Normally the first button on the page takes precedence or nothing happens at all.

To assign default buttons to each form section, simply place them (and any other related controls) inside a PlaceHolder control and set the DefaultButton attribute to the respective button ID.
<asp:Panel DefaultButton="btnSubmit" runat="server"> 
<asp:TextBox ID="tbName" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</asp:Panel>

This rule is not applicable to the LinkButton control (unless in IE).

1 comment:

Anonymous said...

Genial fill someone in on and this enter helped me alot in my college assignement. Say thank you you as your information.