Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SetFocus 1

Status
Not open for further replies.

Dross

Programmer
Aug 16, 2001
212
US
What is the syntax to Set the focus on a textbox in .NET so the cursor starts on the textbox desired?
 
In a webform you need to write java script code to set the initial focus. If you want to do this from the code behind file put this code in your page load

RegisterStartupScript(&quot;focus&quot;, &quot;<script language=&quot;&quot;JavaScript&quot;&quot;>&quot; & vbCrLf & _
vbTab & &quot;Form1.&quot; & XXX.ClientID & &quot;.focus();&quot; & _
vbCrLf & &quot;<&quot; & &quot;/script>&quot;)

Where XXX would stand for your textbox name (id).
and Form1 would be your form name
 
Or you could do this:

Private Sub SetFocus(ByVal ctrl As System.Web.UI.Control)

Dim s As String = &quot;<SCRIPT language='javascript'>document.getElementById('&quot; & ctrl.ID & &quot;').focus() </SCRIPT>&quot;

RegisterStartupScript(&quot;focus&quot;, s)

End Sub

which is a reusable sub that allows you to set the focus of whatever control you pass in.

Check out this thread for more on setting focus:
thread855-400181

D'Arcy
 
how can i do to use this function, or a siilar one, from a web user control ?
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top