Javascript to Set Focus to TextBox

Discussion in 'ASP .Net' started by Joey, Dec 23, 2005.

  1. Joey

    Joey Guest

    I have a javascript piece that sets focus to a textbox on one of my
    webforms. The script works fine for IE: it sets the cursor blinking in
    the textbox. In Firefox, however, it doesn't work at all.

    Below are two snippets from the page source as pulled directly out of
    Firefox after viewing the page:


    I. HTML CODE BLOCK FOR TEXTBOX

    <td align="right" width="100">
    <input name="ctl00$txtUserName" type="text" maxlength="30"
    id="ctl00_txtUserName"
    style="border-width:0px;border-style:None;width:100px;" />
    </td>

    II. SCRIPT CODE BLOCK

    <script>var
    txtBox=document.getElementById("ctl00_txtUserName");if(txtBox!=null)document.all.ctl00_txtUserName.focus();</script>


    There is also another script block immediately following this one.

    I have used this code on a couple of other sites, and it has always
    worked for both browsers. Now, I am having a problem for some reason.
    The only thing new here is that I am now using VS2005 and
    ClientScript.RegisterStartupScript instead of VS2003 and
    Page.RegisterStartupScript.

    Any ideas?
    Joey, Dec 23, 2005
    #1
    1. Advertising

  2. Joey

    albert braun Guest

    i don't know why the script fails on Firefox browsers, but, if you're
    only concerned with setting the focus, you might want to try the
    ASP.NET 2.0 control "Focus" method :

    protected void Page_Load(object sender, EventArgs e)
    {
    this.TextBox2.Focus();
    }

    hth

    seasons greetings,
    albert
    albert braun, Dec 23, 2005
    #2
    1. Advertising

  3. Joey

    Bruce Barker Guest

    firefox, uses the w3c dom, not the IE proprietary dom (.all collection).
    change code to:

    <script>
    var txtBox=document.getElementById("ctl00_txtUserName");
    if (txtBox!=null ) txtBox.focus();
    </script>


    -- bruce (sqlwork.com)


    "Joey" <> wrote in message
    news:...
    >I have a javascript piece that sets focus to a textbox on one of my
    > webforms. The script works fine for IE: it sets the cursor blinking in
    > the textbox. In Firefox, however, it doesn't work at all.
    >
    > Below are two snippets from the page source as pulled directly out of
    > Firefox after viewing the page:
    >
    >
    > I. HTML CODE BLOCK FOR TEXTBOX
    >
    > <td align="right" width="100">
    > <input name="ctl00$txtUserName" type="text" maxlength="30"
    > id="ctl00_txtUserName"
    > style="border-width:0px;border-style:None;width:100px;" />
    > </td>
    >
    > II. SCRIPT CODE BLOCK
    >
    > <script>var
    > txtBox=document.getElementById("ctl00_txtUserName");if(txtBox!=null)document.all.ctl00_txtUserName.focus();</script>
    >
    >
    > There is also another script block immediately following this one.
    >
    > I have used this code on a couple of other sites, and it has always
    > worked for both browsers. Now, I am having a problem for some reason.
    > The only thing new here is that I am now using VS2005 and
    > ClientScript.RegisterStartupScript instead of VS2003 and
    > Page.RegisterStartupScript.
    >
    > Any ideas?
    >
    Bruce Barker, Dec 23, 2005
    #3
  4. How to select all text in this case?
    The tb does not hi-lite it's contents this way.
    I might need to resort to js then.. to bad..



    "albert braun" <> schreef in bericht
    news:...
    >i don't know why the script fails on Firefox browsers, but, if you're
    > only concerned with setting the focus, you might want to try the
    > ASP.NET 2.0 control "Focus" method :
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > this.TextBox2.Focus();
    > }
    >
    > hth
    >
    > seasons greetings,
    > albert
    >
    Edwin Knoppert, Dec 27, 2005
    #4
  5. Joey

    Adrian G

    Joined:
    Sep 6, 2011
    Messages:
    1
    Thank you Bruce.. :)
    Adrian G, Sep 6, 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. mg

    Set focus on a TextBox

    mg, Jul 14, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    485
    dj Bass
    Jul 14, 2003
  2. sumit
    Replies:
    1
    Views:
    905
  3. feng
    Replies:
    5
    Views:
    243,250
    Rajiv R
    Feb 27, 2004
  4. Replies:
    2
    Views:
    336
    Thomas 'PointedEars' Lahn
    Jun 6, 2005
  5. Roger
    Replies:
    3
    Views:
    322
Loading...

Share This Page