ARTICLE

How to Set Focus on a Control in ASP.NET using VB.NET

Posted by John Arcadella Articles | ASP.NET using VB.NET January 18, 2011
How to Set Focus on a Control in ASP.NET using VB.NET
 
Reader Level:


In ASP.NET 4.0, you can use two methods to set focus on controls.

First, by using the control's Focus method. Second, calling page's SetFocus method and passing ID of the control in this SetFocus method.

The following code snippet sets focus on TextBox1 by calling its Focus method on Page load event.

Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs)
TextBox1.Focus()
End Sub 

The following code snippet sets focus on a TextBox control with ID TextBox1.

Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
        SetFocus(TextBox1)
End Sub

You can also set the default focus on a control by using the defaultfocus tag of a Form.

The following code snippet sets default focus on TextBox1.

<form id="form1" runat="server" defaultfocus="TextBox1" >
    <div>
      <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
      <br />
      <asp:Button ID="Button1" runat="server" Text="Button" />
      <br />
    </div>
  </form>

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor