Tuesday 28 February 2012

.Net Beginners : How to specify a default button on the page so that when user hits the Enter key that button gets clicked ?

If you want to set default button page wise , you can use "DefaultButton" Property for Html Form Control.

Syntax :
DefaultButton="button id"

Example :
<form id="frm" DefaultButton="btnSave" runat="server">
.....
</form>
In this when you hits the enter key the click event of btnSave button get clicked.

You can also set multipe Default button means region wise Default button in one page.
For that you use asp:Panel control.
you can use "DefaultButton" Property of asp:Panel control.

Example :
    <asp:Panel runat="server" ID="Panel1" DefaultButton="btnSave">
        <asp:TextBox runat="server" ID="txtSave"></asp:TextBox>
        <asp:Button runat="server" ID="btnSave" Text="Save"   />
    </asp:Panel>
    <asp:Panel runat="server" ID="pnlSearch" DefaultButton="btnSearch">
        <asp:TextBox runat="server" ID="txtSearch"></asp:TextBox>
        <asp:Button runat="server" ID="btnSearch" Text="Search"   />
    </asp:Panel>
In this when your cursor on txtSave textbox and you hit enter at that time btnSave button click event call,
and When your cursor on btnSearch textbox and you hit enter at that time btnSearch button click event call.

No comments:

Post a Comment