Setting Focus to a Control - Web

Discussion in 'ASP .Net' started by Glenn T. Kitchen, Apr 15, 2005.

  1. Hello All,

    Does anyone know how to set the focus to a textbox control on Page_Load?

    Thank you,
    Glenn
    Glenn T. Kitchen, Apr 15, 2005
    #1
    1. Advertising

  2. Patrick Olurotimi Ige, Apr 16, 2005
    #2
    1. Advertising

  3. Or use this:-
    <html>
    <head>
    <script language="JavaScript">
    function Focus()
    {
    formID.urname.focus();
    }
    </script>
    </head>
    <body onload="Focus();">
    <form id="formID">
    Name: <input type="text" id="urname" /><br />
    Age: <input type="text" id="membership" />
    </form>
    </body>
    </html>

    Hope it helps
    Patrick



    *** Sent via Developersdex http://www.developersdex.com ***
    Patrick Olurotimi Ige, Apr 16, 2005
    #3
  4. Thanks Patrick,

    I tried implementing in to the HTML of the webForm the code you specified,
    but didn't do something right and caused me to have to re-program the whole
    page lol. My fault, I don't know HTML very well and was not able to get the
    code back to what it was originally.

    I'll try it again after making a copy of the next WebForm I program in case
    I mess it up again.

    Thanks for you input,
    glenn




    "Patrick Olurotimi Ige" <> wrote in message
    news:#...
    > Or use this:-
    > <html>
    > <head>
    > <script language="JavaScript">
    > function Focus()
    > {
    > formID.urname.focus();
    > }
    > </script>
    > </head>
    > <body onload="Focus();">
    > <form id="formID">
    > Name: <input type="text" id="urname" /><br />
    > Age: <input type="text" id="membership" />
    > </form>
    > </body>
    > </html>
    >
    > Hope it helps
    > Patrick
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    Glenn T. Kitchen, Apr 16, 2005
    #4
  5. Patrick Olurotimi Ige, Apr 28, 2005
    #5
  6. Glenn T. Kitchen

    Toby Guest

    Try this:-

    Public Shared Sub SetFocus(ByVal control As Control)
    If control.Page Is Nothing Then
    Throw New ArgumentException("The Control must be added to a Page
    before you can set the IntialFocus to it.")
    End If
    'If the control is enabled and visible select it
    If control.Visible And control.EnableViewState Then
    If control.Page.Request.Browser.JavaScript = True Then
    ' Create JavaScript
    Dim s As StringBuilder = New StringBuilder
    s.Append(vbCrLf + "<SCRIPT LANGUAGE='JavaScript'>" + vbCrLf)
    s.Append("<!--" + vbCrLf)
    s.Append("function SetInitialFocus()" + vbCrLf)
    s.Append("{" + vbCrLf)
    s.Append(" try " & vbCrLf)
    s.Append(" {" & vbCrLf)
    s.Append(" document.")
    ' Find the Form
    Dim p As control = control.Parent
    While Not (TypeOf p Is System.Web.UI.HtmlControls.HtmlForm)
    p = p.Parent
    End While
    s.Append(p.ClientID)
    s.Append("['")
    s.Append(control.UniqueID)
    ' Set Focus on the selected item of a RadioButtonList
    If TypeOf control Is RadioButtonList Then
    Dim rbl As RadioButtonList = control
    If Not rbl Is Nothing Then
    Dim suffix As String = "_0"
    Dim t As Int32 = 0
    Dim li As ListItem
    For Each li In rbl.Items
    If li.Selected Then
    suffix = "_" + t.ToString()
    Exit For
    End If
    t = t + 1
    Next
    s.Append(suffix)
    End If
    End If
    ' Set Focus on the first item of a CheckBoxList
    If TypeOf control Is CheckBoxList Then
    s.Append("_0")
    End If
    s.Append("'].focus();" + vbCrLf)
    s.Append(" }catch(e){}" & vbCrLf) ' close the try, catch
    block
    s.Append("}" + vbCrLf)
    If control.Page.SmartNavigation Then
    s.Append("window.setTimeout(SetInitialFocus, 500);" +
    vbCrLf)
    Else
    s.Append("window.onload = SetInitialFocus;" + vbCrLf)
    End If
    s.Append("// -->" + vbCrLf)
    s.Append("</SCRIPT>" + vbCrLf)
    ' Register Client Script
    control.Page.RegisterClientScriptBlock("InitialFocus",
    s.ToString())
    End If
    End If
    End Sub

    Regards

    Toby.

    "Glenn T. Kitchen" <> wrote in message
    news:...
    > Hello All,
    >
    > Does anyone know how to set the focus to a textbox control on Page_Load?
    >
    > Thank you,
    > Glenn
    >
    >
    Toby, Jun 14, 2005
    #6
    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. Vinod I
    Replies:
    2
    Views:
    411
    Peter Blum
    Feb 4, 2004
  2. =?Utf-8?B?Q0xFQVItUkNJQw==?=

    setting focus to an asp.net web user control

    =?Utf-8?B?Q0xFQVItUkNJQw==?=, Dec 6, 2004, in forum: ASP .Net
    Replies:
    12
    Views:
    6,121
    Patrick Olurotimi Ige
    Dec 7, 2004
  3. Jason
    Replies:
    4
    Views:
    1,618
    Jason
    May 7, 2007
  4. Colin Halliday

    Setting Focus in Web User Control that Uses AJAX Update Panel

    Colin Halliday, Apr 24, 2007, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    193
    Colin Halliday
    Apr 24, 2007
  5. Roger
    Replies:
    3
    Views:
    322
Loading...

Share This Page