Setting Focus on an asp:textbox

Discussion in 'ASP .Net' started by George Durzi, Aug 20, 2003.

  1. George Durzi

    George Durzi Guest

    I'm trying to set focus on an asp:textbox. I'm running this code after my
    page_load

    StringBuilder sb = new StringBuilder();

    sb.Append(@"<script language=javascript>");
    sb.Append(@"<!--");
    sb.Append(@"document.getElementById(txtUserName).focus();");
    sb.Append(@"-->");
    sb.Append(@"</script>");

    if (!IsStartupScriptRegistered("LoginFocus"))
    RegisterStartupScript("LoginFocus", sb.ToString());

    If I View Source, I can confirm that the JavaScript is being outputed before
    the </form> tag, but my control is not getting focus ...

    Any idea?
     
    George Durzi, Aug 20, 2003
    #1
    1. Advertising

  2. George Durzi

    George Durzi Guest

    As far as my understanding of RegisterStartupScript goes, this script is
    rendered as the last thing before the </form> tag, so it's rendered AFTER
    the text box was rendered.


    "Marina" <> wrote in message
    news:%...
    > That is because your script is running before the textbox has been

    rendered.
    > Make this scrip the onload handler for the window object.
    >
    > "George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
    > news:...
    > > I'm trying to set focus on an asp:textbox. I'm running this code after

    my
    > > page_load
    > >
    > > StringBuilder sb = new StringBuilder();
    > >
    > > sb.Append(@"<script language=javascript>");
    > > sb.Append(@"<!--");
    > > sb.Append(@"document.getElementById(txtUserName).focus();");
    > > sb.Append(@"-->");
    > > sb.Append(@"</script>");
    > >
    > > if (!IsStartupScriptRegistered("LoginFocus"))
    > > RegisterStartupScript("LoginFocus", sb.ToString());
    > >
    > > If I View Source, I can confirm that the JavaScript is being outputed

    > before
    > > the </form> tag, but my control is not getting focus ...
    > >
    > > Any idea?
    > >
    > >

    >
    >
     
    George Durzi, Aug 20, 2003
    #2
    1. Advertising

  3. George Durzi

    George Durzi Guest

    Yes, and it's rendering at the end as defined by the RegisterStartupScript
    method. In my first message, I posted
    > > > > If I View Source, I can confirm that the JavaScript is being

    outputed
    > > > before
    > > > > the </form> tag, but my control is not getting focus ...


    "Marina" <> wrote in message
    news:...
    > I believe it is rendered in the beginning - not end.
    >
    > The best way to check would be to look at the rendered HTML source -
    > wouldn't it?
    >
    > "George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
    > news:...
    > > As far as my understanding of RegisterStartupScript goes, this script is
    > > rendered as the last thing before the </form> tag, so it's rendered

    AFTER
    > > the text box was rendered.
    > >
    > >
    > > "Marina" <> wrote in message
    > > news:%...
    > > > That is because your script is running before the textbox has been

    > > rendered.
    > > > Make this scrip the onload handler for the window object.
    > > >
    > > > "George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
    > > > news:...
    > > > > I'm trying to set focus on an asp:textbox. I'm running this code

    after
    > > my
    > > > > page_load
    > > > >
    > > > > StringBuilder sb = new StringBuilder();
    > > > >
    > > > > sb.Append(@"<script language=javascript>");
    > > > > sb.Append(@"<!--");
    > > > > sb.Append(@"document.getElementById(txtUserName).focus();");
    > > > > sb.Append(@"-->");
    > > > > sb.Append(@"</script>");
    > > > >
    > > > > if (!IsStartupScriptRegistered("LoginFocus"))
    > > > > RegisterStartupScript("LoginFocus", sb.ToString());
    > > > >


    > > > >
    > > > > Any idea?
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    George Durzi, Aug 20, 2003
    #3
  4. George Durzi

    George Durzi Guest

    It worked. Very strange. the only think I changed was removing
    sb.Append(@"<!--");
    and
    sb.Append(@"-->");

    "George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
    news:%...
    > Yes, and it's rendering at the end as defined by the RegisterStartupScript
    > method. In my first message, I posted
    > > > > > If I View Source, I can confirm that the JavaScript is being

    > outputed
    > > > > before
    > > > > > the </form> tag, but my control is not getting focus ...

    >
    > "Marina" <> wrote in message
    > news:...
    > > I believe it is rendered in the beginning - not end.
    > >
    > > The best way to check would be to look at the rendered HTML source -
    > > wouldn't it?
    > >
    > > "George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
    > > news:...
    > > > As far as my understanding of RegisterStartupScript goes, this script

    is
    > > > rendered as the last thing before the </form> tag, so it's rendered

    > AFTER
    > > > the text box was rendered.
    > > >
    > > >
    > > > "Marina" <> wrote in message
    > > > news:%...
    > > > > That is because your script is running before the textbox has been
    > > > rendered.
    > > > > Make this scrip the onload handler for the window object.
    > > > >
    > > > > "George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
    > > > > news:...
    > > > > > I'm trying to set focus on an asp:textbox. I'm running this code

    > after
    > > > my
    > > > > > page_load
    > > > > >
    > > > > > StringBuilder sb = new StringBuilder();
    > > > > >
    > > > > > sb.Append(@"<script language=javascript>");
    > > > > > sb.Append(@"<!--");
    > > > > > sb.Append(@"document.getElementById(txtUserName).focus();");
    > > > > > sb.Append(@"-->");
    > > > > > sb.Append(@"</script>");
    > > > > >
    > > > > > if (!IsStartupScriptRegistered("LoginFocus"))
    > > > > > RegisterStartupScript("LoginFocus", sb.ToString());
    > > > > >

    >
    > > > > >
    > > > > > Any idea?
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    George Durzi, Aug 20, 2003
    #4
  5. George Durzi

    Marina Guest

    Ah, sorry, was thinking of the RegisterClientScriptBlock method.

    "George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
    news:%...
    > Yes, and it's rendering at the end as defined by the RegisterStartupScript
    > method. In my first message, I posted
    > > > > > If I View Source, I can confirm that the JavaScript is being

    > outputed
    > > > > before
    > > > > > the </form> tag, but my control is not getting focus ...

    >
    > "Marina" <> wrote in message
    > news:...
    > > I believe it is rendered in the beginning - not end.
    > >
    > > The best way to check would be to look at the rendered HTML source -
    > > wouldn't it?
    > >
    > > "George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
    > > news:...
    > > > As far as my understanding of RegisterStartupScript goes, this script

    is
    > > > rendered as the last thing before the </form> tag, so it's rendered

    > AFTER
    > > > the text box was rendered.
    > > >
    > > >
    > > > "Marina" <> wrote in message
    > > > news:%...
    > > > > That is because your script is running before the textbox has been
    > > > rendered.
    > > > > Make this scrip the onload handler for the window object.
    > > > >
    > > > > "George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
    > > > > news:...
    > > > > > I'm trying to set focus on an asp:textbox. I'm running this code

    > after
    > > > my
    > > > > > page_load
    > > > > >
    > > > > > StringBuilder sb = new StringBuilder();
    > > > > >
    > > > > > sb.Append(@"<script language=javascript>");
    > > > > > sb.Append(@"<!--");
    > > > > > sb.Append(@"document.getElementById(txtUserName).focus();");
    > > > > > sb.Append(@"-->");
    > > > > > sb.Append(@"</script>");
    > > > > >
    > > > > > if (!IsStartupScriptRegistered("LoginFocus"))
    > > > > > RegisterStartupScript("LoginFocus", sb.ToString());
    > > > > >

    >
    > > > > >
    > > > > > Any idea?
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Marina, Aug 20, 2003
    #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. test test

    setting focus to a textbox

    test test, Aug 30, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,787
    Kevin Spencer
    Aug 30, 2003
  2. Jason
    Replies:
    4
    Views:
    1,683
    Jason
    May 7, 2007
  3. Jesper Dannemamm

    HELP: setting focus asp:textbox in a user control

    Jesper Dannemamm, Dec 6, 2003, in forum: ASP General
    Replies:
    3
    Views:
    231
    Aaron Bertrand [MVP]
    Dec 6, 2003
  4. Replies:
    2
    Views:
    359
    Thomas 'PointedEars' Lahn
    Jun 6, 2005
  5. Roger
    Replies:
    3
    Views:
    379
Loading...

Share This Page