How To Focus Textbox

mmb

Active member
Joined
Aug 1, 2004
Messages
42
Programming Experience
1-3
IN ASP.NET application how to bring the Cursor in the textbox (server control) at the start of the application.
 
That link shows C# code. In case you need it, here's the VB.NET translation:

VB.NET:
Private Sub Page_Load(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles MyBase.Load
    SetFocus(TextBox2)
End Sub

Protected Sub SetFocus(ByVal ctrl As System.Web.UI.Control)
    Dim s As String = "<SCRIPT language='javascript'>document.getElementById('" _
      & ctrl.ID & "').focus() </SCRIPT>"
    Me.RegisterStartupScript("focus", s)
End Sub

The method RegisterStartupScript allows ASP.NET server controls to emit client-side script blocks (in this case javaScript) in the System.Web.UI.Page.
 
Back
Top