Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Moving the focus on a control when the page loads

Total Hit ( 2614)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


ASP.NET doesn't provide a built-in method for giving a control the input focus when the page loads. In fact, you can assign the TabIndex properties to controls on a page, but you can't use this property to set the initial focus on a control of your choice.

The following procedure provides you with the ability to do exactly this

Click here to copy the following block
Sub SetControlFocus(ByVal ctrl As Control)
  Dim code As String = String.Format("<script language='javascript'>" & _
    "document.getElementById(""{0}"").focus();</script>", ctrl.ClientID)
  ctrl.Page.RegisterStartupScript("SetControlFocus", code)
End Sub

As you see, the procedure inserts a line of JavaScript that runs after the page has been loaded, and that gives the focus to the control you pass as an argument. Because the Page object is retrieved as a property of the control, you can store this routine in any module of your ASP.NET application. You typicall call this procedure from the Page_Load event:

Click here to copy the following block
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles _
  MyBase.Load
  SetControlFocus( TextBox2 )
End Sub



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.