Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Set Default Button and Default Focus in ASP.NET application

Article:
Viewed:  1316 
Posted On:  25/07/2013 11:25:17 
How we can set default button click and default focus in asp.net application....? 

In this article I am going to show how we can set default focus and default button in asp.net.

For this we need to set property in <form> tag... like below

<form id="form1" runat="server" defaultbutton="btnRegister" defaultfocus="txtName">

Output:

When page load then default focus set:

Default Focus.png

Image 1.

If you press enter then default button is register...

Default Button.png

Image 2.

ASPX code is:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Set Default Button & Default Focus in ASP.NET</title>
</head>
<body>
    <form id="form1" runat="server" defaultbutton="btnRegister" defaultfocus="txtName">
    <div>
        <table cellpadding="4" cellspacing="4" width="70%" align="center" style="border: solid 1px Gray;">
            <tr>
                <td align="right" width="20%">
                    Name:
                </td>
                <td>
                    &nbsp;
                    <asp:TextBox ID="txtName" runat="server" Width="200px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvName" runat="server" ControlToValidate="txtName"
                        ErrorMessage="Enter Name"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td align="right" width="20%">
                    Email:
                </td>
                <td>
                    &nbsp;
                    <asp:TextBox ID="txtEmail" runat="server" Width="200px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtEmail"
                        ErrorMessage="Enter Email"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td align="right" width="20%">
                    Password:
                </td>
                <td>
                    &nbsp;
                    <asp:TextBox ID="txtPassword" runat="server" Width="200px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="right" width="40%">
                    Mobile:
                </td>
                <td>
                    &nbsp;
                    <asp:TextBox ID="txtMobile" runat="server" Width="200px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Button ID="btnRegister" runat="server" Text="Register" Width="140px" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us