focusing on a textbox

Discussion in 'ASP .Net' started by Steven Zilberman, Jul 17, 2003.

  1. Is there a way to focus on a textbox in ASP.Net?
    Can I do something like textBoxMyTextBox.Focus() ???
    Meaning, I can't do it with the JavaScript way, because
    JavaScript does not know the form name, and for some
    reason it does not see the id of the textbox. Thank you.
    Steven Zilberman, Jul 17, 2003
    #1
    1. Advertising

  2. Steven Zilberman

    Steven Guest

    For some reason it's not working. My asp code looks like
    this:
    <form runat="server">
    <asp:TextBox id="UserName" runat="server" MaxLength="10"
    Width="120px"></asp:TextBox>
    </form>

    The JavaScript code looks like this:
    <script language="JavaScript">
    <!--
    window.document.all("UserName").focus();
    //-->
    </script>

    I am getting a javascript error which says that
    window.document.all(...) is a null or not an object.

    Thanks.

    Steven
    >-----Original Message-----
    >Hi,
    >
    >I don't believe there is a way to do this with server-

    side
    >programming. However, doesn't the JavaScript
    >window.document.all("textBoxMyTextBox").focus(); work?
    >
    >Greetz,
    >Domien
    >
    >>-----Original Message-----
    >>Is there a way to focus on a textbox in ASP.Net?
    >>Can I do something like textBoxMyTextBox.Focus() ???
    >>Meaning, I can't do it with the JavaScript way, because
    >>JavaScript does not know the form name, and for some
    >>reason it does not see the id of the textbox. Thank

    you.
    >>.
    >>

    >.
    >
    Steven, Jul 17, 2003
    #2
    1. Advertising

  3. Hi

    Probably the element is not ready/loaded on the client when you call
    focus....
    Try changing the script to this... should be easy to modify to focus on any
    element...

    <script language="JavaScript">
    <!--
    function doFocus(){
    if(document.forms[0].UserName) document.forms[0].UserName.focus();
    }
    onload = doFocus;
    //-->
    </script>



    --
    Best Regards
    Vidar Petursson
    ==============================
    Microsoft Internet Client & Controls MVP
    ==============================
    "Steven" <> wrote in message
    news:09a601c34c68$2688dfc0$...
    > For some reason it's not working. My asp code looks like
    > this:
    > <form runat="server">
    > <asp:TextBox id="UserName" runat="server" MaxLength="10"
    > Width="120px"></asp:TextBox>
    > </form>
    >
    > The JavaScript code looks like this:
    > <script language="JavaScript">
    > <!--
    > window.document.all("UserName").focus();
    > //-->
    > </script>
    >
    > I am getting a javascript error which says that
    > window.document.all(...) is a null or not an object.
    >
    > Thanks.
    >
    > Steven
    > >-----Original Message-----
    > >Hi,
    > >
    > >I don't believe there is a way to do this with server-

    > side
    > >programming. However, doesn't the JavaScript
    > >window.document.all("textBoxMyTextBox").focus(); work?
    > >
    > >Greetz,
    > >Domien
    > >
    > >>-----Original Message-----
    > >>Is there a way to focus on a textbox in ASP.Net?
    > >>Can I do something like textBoxMyTextBox.Focus() ???
    > >>Meaning, I can't do it with the JavaScript way, because
    > >>JavaScript does not know the form name, and for some
    > >>reason it does not see the id of the textbox. Thank

    > you.
    > >>.
    > >>

    > >.
    > >
    Vidar Petursson, Jul 17, 2003
    #3
  4. "Steven" <> wrote in
    news:09a601c34c68$2688dfc0$:

    > For some reason it's not working. My asp code looks like
    > this:
    ><form runat="server">
    ><asp:TextBox id="UserName" runat="server" MaxLength="10"
    > Width="120px"></asp:TextBox>
    ></form>
    >
    > The JavaScript code looks like this:
    ><script language="JavaScript">
    ><!--
    > window.document.all("UserName").focus();
    > //-->
    ></script>
    >
    > I am getting a javascript error which says that
    > window.document.all(...) is a null or not an object.


    Steven,

    Look at the browser's HTML source for your page. I'll bet ASP.NET
    modified the control name to something like _ctl0_UserName. ASP.NET
    usually does this to controls in a UserControl (.ascx), so there
    won't be a name collision if multiple instances of the same
    UserControl are on the same page.

    The way to fix this is to insert the JavaScript using server-side
    code, and use the control's ClientId property to refer to the control
    in the JavaScript code.

    For example, you can put code like this in the Page_Load event of
    your form (C#) to make the UserName TextBox control get focus:

    string script = @"
    <script language=""javascript"">
    <!--
    document.getElementById('{0}').focus();
    //-->
    </script>
    ";

    if (!this.IsStartupScriptRegistered("SetWebControlFocus"))
    this.RegisterStartupScript("SetWebControlFocus",
    string.Format(script, UserName.ClientID));



    Hope this helps.

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.
    http://www.crtimmonsinc.com/
    Chris R. Timmons, Jul 17, 2003
    #4
    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. Ryan Stewart

    Focusing a JPanel?

    Ryan Stewart, Dec 4, 2003, in forum: Java
    Replies:
    0
    Views:
    402
    Ryan Stewart
    Dec 4, 2003
  2. problem child

    focusing a spawned application

    problem child, Oct 9, 2004, in forum: Python
    Replies:
    0
    Views:
    263
    problem child
    Oct 9, 2004
  3. Bob Greschke

    Tkinter window focusing or selecting

    Bob Greschke, Sep 4, 2006, in forum: Python
    Replies:
    0
    Views:
    291
    Bob Greschke
    Sep 4, 2006
  4. Ricardo Garcia

    problem with focusing in frames

    Ricardo Garcia, Sep 5, 2004, in forum: Javascript
    Replies:
    1
    Views:
    79
  5. Milan Kuèera

    Focusing win32 application (exe) from browser?

    Milan Kuèera, Mar 21, 2005, in forum: Javascript
    Replies:
    0
    Views:
    137
    Milan Kuèera
    Mar 21, 2005
Loading...

Share This Page