Asp.net: Setting DefaultButton and Defaultfocus in webform and content page

In this article I am going to explain how to set the defaultbutton for enter key press and defaultfocus in textbox

Description:
We use the DefaultButton and Defaultfocus to set the specific button and textbox as default from multiple textboxes and buttons.

Implementation:

Default button and default focus in form
HML Markup:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server" defaultbutton="btnsubmit" defaultfocus="txtfirstname">

    <fieldset style="width:25%">
    <legend></legend>
    <table>
    <tr><td>First Name:</td><td><asp:TextBox ID="txtfirstname" runat="server"></asp:TextBox></td></tr>
     <tr><td>Last Name: </td><td><asp:textbox ID="txtlastname" runat="server"></asp:textbox></td></tr>
     <tr><td></td><td></td></tr>
      <tr><td></td><td> <asp:Button ID="btnsubmit" runat="server" Text="Submit" onclick="btnsubmit_Click" /> <asp:Button ID="btnclear"
            runat="server" Text="Clear" onclick="btnclear_Click"  /></td></tr>
    </table>
    </fieldset>
    </form>
</body>
</html>

We can set the Default button and default focus from code behind. For this we have to provide the button id and textbox id.
C# Code:
protected void Page_Load(object sender, EventArgs e)
    {
       this.Page.Form.DefaultButton = btnsubmit.UniqueID;
       this.Page.Form.DefaultFocus = txtfirstname.ClientID;       
    }

VB.net Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Me.Page.Form.DefaultButton = btnsubmit.UniqueID
Me.Page.Form.DefaultFocus = txtfirstname.ClientID
End Sub


Default button and default focus in Content page
HTML Markup:
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
 <fieldset style="width:25%">
    <legend></legend>
    <table>
    <tr><td>First Name:</td><td><asp:TextBox ID="txtfirstname" runat="server"></asp:TextBox></td></tr>
     <tr><td>Last Name: </td><td><asp:textbox ID="txtlastname" runat="server"></asp:textbox></td></tr>
     <tr><td></td><td></td></tr>
      <tr><td></td><td> <asp:Button ID="btnsubmit" runat="server" Text="Submit" onclick="btnsubmit_Click" /> <asp:Button ID="btnclear"
            runat="server" Text="Clear" onclick="btnclear_Click"  /></td></tr>
    </table>  
    </fieldset>
</asp:Content>


C# Code:
protected void Page_Load(object sender, EventArgs e)
    {
       this.Page.Form.DefaultButton = btnsubmit.UniqueID;
       this.Page.Form.DefaultFocus = txtfirstname.ClientID;       
    }

VB.net Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Me.Page.Form.DefaultButton = btnsubmit.UniqueID
Me.Page.Form.DefaultFocus = txtfirstname.ClientID
End Sub

Default button and default focus in Panel
We can also set the Default button and default focus in panel using Default button and default focus property of panel.
HTML Markup:
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
 <fieldset style="width:25%">
    <legend></legend>
     <asp:Panel ID="Panel1" runat="server" Defaultbutton="btnsubmit" Defaultfocus="txtfirstname">  
     <table>
    <tr><td>First Name:</td><td><asp:TextBox ID="txtfirstname" runat="server"></asp:TextBox></td></tr>
     <tr><td>Last Name: </td><td><asp:textbox ID="txtlastname" runat="server"></asp:textbox></td></tr>
     <tr><td></td><td></td></tr>
      <tr><td></td><td> <asp:Button ID="btnsubmit" runat="server" Text="Submit" onclick="btnsubmit_Click" /> <asp:Button ID="btnclear"
            runat="server" Text="Clear" onclick="btnclear_Click"  /></td></tr>
    </table>  
     </asp:Panel>
    </fieldset>
</asp:Content>


C# Code:
protected void Page_Load(object sender, EventArgs e)
    {
        this.Page.Form.DefaultButton = btnsubmit.UniqueID;
        this.Page.Form.DefaultFocus = txtfirstname.ClientID; 
    }

VB.net Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Me.Page.Form.DefaultButton = btnsubmit.UniqueID
Me.Page.Form.DefaultFocus = txtfirstname.ClientID
End Sub 

Post a Comment

0 Comments