focusing on a textbox

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default focusing on a textbox

    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 Guest

  2. Similar Questions and Discussions

    1. Focusing a TextInput control
      Solved this issue: Had to use callLater(), to focus the component during the next event loop. Stefan http://www.justRIA.com/
    2. Activating (focusing) new bookmark?
      I just downloaded the Acrobat 8 SDK (now free yea!) and am writing my first Windows plug-in to enhance Acrobat 8's bookmarking capability. I can...
    3. Focusing more on Ruby ... Windows ver?
      OK, after this C# book is over, I'll be getting back into Ruby more. It's more fun in a number of ways. Martin Fowler, who got me using XSLT, has...
    4. Focusing...
      Hi, Quick question. With a manual focus camera, would it be better to focus with or with out one's glasses. I've got an older slr, and I've...
    5. Canon A1 Focusing Screens
      G'day Everyone, I recently acquired a Canon A1 SLR, which goes along well with my AE1 and AV1. My question is, does anybody know if Canon...
  3. #2

    Default focusing on a textbox

    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 Guest

  4. #3

    Default Re: focusing on a textbox

    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" <stevenzilberman@alum.rpi.edu> wrote in message
    news:09a601c34c68$2688dfc0$a501280a@phx.gbl...
    > 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 Guest

  5. #4

    Default Re: focusing on a textbox

    "Steven" <stevenzilberman@alum.rpi.edu> wrote in
    news:09a601c34c68$2688dfc0$a501280a@phx.gbl:
    > 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("SetWebControlFoc us"))
    this.RegisterStartupScript("SetWebControlFocus",
    string.Format(script, UserName.ClientID));



    Hope this helps.

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.
    [url]http://www.crtimmonsinc.com/[/url]
    Chris R. Timmons Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139