Set DefaultButton for enter key press in Asp.Net MasterPage’s Content page and set DefaultFocus in textbox

Introduction: In this article I am going to explain how to set specific button as default button among multiple buttons to respond to enter key press  and set focus in specific text box on page load in Master Page’s Content page.

Set DefaultButton on enter key press in Asp.Net MasterPage’s Content page and set DefaultFocus in textbox

Description:  When there are multiple input controls (textbox) and multiple buttons and we want to set focus in specific text box and set a button as default. For example we have a search option in web page having a textbox as shown in above image and we want to trigger search button on enter key press then we can achieve this using any of the following two ways:
  1. By setting DefaultFocus and DefaultButton property through code in code file.
  2. By wrapping all the controls in Panel control and setting its DefaultButton property. 

Implementation: Let’s create an example page to see these two tricks in action one by one

HTML Source Code:

Design the page as shown in image by copying the following HTML Source

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">   
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
            <fieldset style="width:230px;">
        <legend>           
            Set Default Focus and Default Button
            </legend>
        <table>
            <tr><td><asp:TextBox ID="txtSearch" runat="server" ></asp:TextBox></td>
                <td><asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" /></td>
                <td><asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" /></td>
            </tr>           
        </table>
    </fieldset>
    </asp:Content>


Setting DefaultFocus and DefaultButton property through code in code file as:

protected void Page_Load(object sender, EventArgs e)
    {
        this.form1.DefaultFocus = txtSearch.ClientID;
        this.Form.DefaultButton = btnSearch.UniqueID;
    }


Placing all the controls in Panel control and setting its DefaultButton property:


<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">   
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Panel ID="Panel1" runat="server" DefaultButton="btnSearch">
        <fieldset style="width:230px;">
        <legend>           
            Set Default Focus and Default Button
            </legend>
        <table>
            <tr><td><asp:TextBox ID="txtSearch" runat="server" ></asp:TextBox></td>
                <td><asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" /></td>
                <td><asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" /></td>
            </tr>           
        </table>
    </fieldset>
        </asp:Panel>
</asp:Content> 

Note: we can just set DefaultButton property in panel and because there is no DefaultFocus property.  One point to note is that DefaultButton will only work if the focus is in input control i.e. Textbox. So we need to set focus in textbox also as:

  • In page load event of code file write as: 

protected void Page_Load(object sender, EventArgs e)
    {
        //Set default focus in Search textbox
        this.Form.DefaultFocus = txtSearch.ClientID;
        //or you can also set focus in search textbox as:
        //txtSearch.Focus();
    } 

Now over to you:
" I hope you have got how to Set Default Focus in textbox and Default Button on enter key press in content page in asp.net and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..