Powered by Blogger.

Saturday, March 8, 2014

Set default focus in Asp.net




In this post we will discuss how we can set default focus to controls in Asp.Net.

Also you can check out:

- Filter Attribute in MVC

- Working with enterprise library for data access in asp.net Part-2

- Tutorial on MVVM with WPF

To set focus on an ASP.NET Web server control, you can call the Focus method of the web server control. The focus method id available to controls like: Button, LinkButton, ImageButton, CheckBox, DropDownList, FileUpload, HyperLink, ListBox, RadioButton, TextBox etc.

You can set the focus of control at runtime like below:

protected void Page_Load(object sender, EventArgs e)
{
    txtUserName.Focus();
}

Also you can set focus by using the SetFocus method like below:
protected void Page_Load(object sender, EventArgs e)
    {    
      Page.SetFocus(txtUserName);
    }

To set default focus in a form:

To set the focus in a form do like below:

<form id="form1" runat="server" defaultfocus="txtUserName" >
    <div>
      <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>    
    </div>
  </form>



0 comments

Post a Comment