Set focus to a textbox on page load

Discussion in 'ASP .Net' started by Water Cooler v2, Aug 17, 2005.

  1. I am new to ASP/ASP.NET so kindly be gentle. When my index.aspx page
    loads, I need the focus to be on one of the textboxes called
    txtUserName.

    I come from a VB background, so expecting the obvious, I go to the
    Page_Load event of the index.aspx page and try to write
    txtUserName.SetFocus but I see there isn't a SetFocus method for the
    System.Web.UI.WebControls.TextBox class.

    What's the way to set the focus to a textbox on the page load in
    ASP.NET?
     
    Water Cooler v2, Aug 17, 2005
    #1
    1. Advertising

  2. you can write a function something like this and use it when u need them.

    protected void SetFocus(Control controlToFocus){
    string formName = GetFormName(controlToFocus);
    string jsString="<script language=javascript>document." + formName +
    ".elements['" + controlToFocus.UniqueID + "'].focus();</script>";
    if(Page.IsStartupScriptRegistered("SetFocusToSearch")==false)
    Page.RegisterStartupScript("SetFocusToSearch",jsString);

    Hope this helps.
    --
    Kannan.V
    Home : http://www.kannanv.com
    Blog : http://kannanv.blogspot.com
    Web : http://www.DotnetLounge.net

    "Any one who has never made a mistake has never tried anything new" - Einstein


    "Water Cooler v2" wrote:

    > I am new to ASP/ASP.NET so kindly be gentle. When my index.aspx page
    > loads, I need the focus to be on one of the textboxes called
    > txtUserName.
    >
    > I come from a VB background, so expecting the obvious, I go to the
    > Page_Load event of the index.aspx page and try to write
    > txtUserName.SetFocus but I see there isn't a SetFocus method for the
    > System.Web.UI.WebControls.TextBox class.
    >
    > What's the way to set the focus to a textbox on the page load in
    > ASP.NET?
    >
    >
     
    =?Utf-8?B?S2FubmFuLlYgW01DU0QubmV0XQ==?=, Aug 17, 2005
    #2
    1. Advertising

  3. As the last post adviced this should do the trick:-

    Private Sub SetFocus(ByVal ctrl As Control)
    ' Define the JavaScript function for the specified control.
    Dim focusScript As String = "<script language='javascript'>" & _
    "document.getElementById('" + ctrl.ClientID & _
    "').focus();</script>"

    ' Add the JavaScript code to the page.
    Page.RegisterStartupScript("FocusScript", focusScript)
    End Sub

    Hope this helps
    Patrick



    *** Sent via Developersdex http://www.developersdex.com ***
     
    Patrick Olurotimi Ige, Aug 17, 2005
    #3
  4. It's a client-side task. You need to call javascript focus() method on the
    element you want to set focus on.

    Eliyahu

    "Water Cooler v2" <> wrote in message
    news:...
    > I am new to ASP/ASP.NET so kindly be gentle. When my index.aspx page
    > loads, I need the focus to be on one of the textboxes called
    > txtUserName.
    >
    > I come from a VB background, so expecting the obvious, I go to the
    > Page_Load event of the index.aspx page and try to write
    > txtUserName.SetFocus but I see there isn't a SetFocus method for the
    > System.Web.UI.WebControls.TextBox class.
    >
    > What's the way to set the focus to a textbox on the page load in
    > ASP.NET?
    >
     
    Eliyahu Goldin, Aug 17, 2005
    #4
  5. Water Cooler v2

    drujba

    Joined:
    Jan 13, 2011
    Messages:
    1
    focus

    just add into the Page-Load function the following:

    txUserName.Focus();

    works for me.. cheers!



     
    drujba, Jan 13, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kanna
    Replies:
    0
    Views:
    420
    Kanna
    Dec 10, 2003
  2. Justin Dutoit
    Replies:
    0
    Views:
    1,147
    Justin Dutoit
    Feb 27, 2009
  3. Kanna
    Replies:
    0
    Views:
    142
    Kanna
    Dec 10, 2003
  4. Replies:
    2
    Views:
    360
    Thomas 'PointedEars' Lahn
    Jun 6, 2005
  5. Roger
    Replies:
    3
    Views:
    380
Loading...

Share This Page