Problem with focusing TextBox when Page loads

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NawazAhmed
    New Member
    • Feb 2008
    • 36

    Problem with focusing TextBox when Page loads

    Hi,
    I am working with Visual Studio 2003, framework 1.1
    I am trying to focus a textbox when page loads. I tried
    [code=vbnet]
    Page.RegisterSt artupScript("fo cus", "<script>docume nt.getElementBy Id('name').focu s();</script>")
    and
    textbox.focus()
    and
    textbox.setfocu s()

    [/code]

    but it says focus or setfocus is not there in system.web.ui.c ontrols.textbox
    I have also imported system.text

    Can someone help me with this.

    Thanks and Regards.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You seem to be mixing your behaviours considerably.
    What you want is a javascript function to run in the "onload" event of the page.
    In that function you would set the focus of the control.

    Something like:
    [code=javascript]
    function myfocus()
    {
    var mytext = document.getEle mentById("mytex t2");
    mytext.focus();
    mytext.select() ;
    }
    [/code]

    Then assign it to the onload attribute of your page.

    Comment

    • NawazAhmed
      New Member
      • Feb 2008
      • 36

      #3
      Hi, Thanks for the reply.
      As u mentioned, the javascript code should be in the code behind or seperate javascript file?
      What abt onload event is it the one which is in the code behind???If yes then how will u write the javascript code in code behind?
      And I am using asp button not the html one.
      How you will be implementing this could u give me a complete example.

      Thanks.
      Originally posted by Plater
      You seem to be mixing your behaviours considerably.
      What you want is a javascript function to run in the "onload" event of the page.
      In that function you would set the focus of the control.

      Something like:
      [code=javascript]
      function myfocus()
      {
      var mytext = document.getEle mentById("mytex t2");
      mytext.focus();
      mytext.select() ;
      }
      [/code]

      Then assign it to the onload attribute of your page.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        You said on page load, nothing about a button press, so I am giving you the examples for how to do it on page load.
        Stick the javascript in your aspx file. Then add the onload attribute to your body element.
        [code=html]
        <body onload="myfocus ();" >
        [/code]

        For a button press you would do different things.

        Comment

        Working...