Setting focus on TextBox

Discussion in 'ASP .Net Web Controls' started by Guinness Mann, Dec 3, 2003.

  1. Greetings,

    I have a textbox in my aspx file:

    <INPUT id="txtNotes" runat="server" style="Z-INDEX: 103; LEFT: 0px;
    WIDTH: 749px; POSITION: absolute; TOP: 0px; HEIGHT: 192px"
    type="text" size="119" value="Error Notes">

    and a link in my aspx.cs file:

    protected HtmlInputText txtNotes;

    Normally, txtNotes is invisible, but at certain points in the program I
    want to set Visible to true and set focus on the control.

    I can make everything work except for the part about setting focus. Is
    there anything I can do from the server side to set focus to my text
    control in response to a button press?

    -- Rick
    Guinness Mann, Dec 3, 2003
    #1
    1. Advertising

  2. Rick,

    Yes. You will need to use RegisterStartupScript to add a client-side
    script to set the focus. Here's an example:

    System.Text.StringBuilder sb = new System.Text.StringBuilder("");

    sb.Append("<script language=\"JavaScript\">");
    sb.Append(" document.forms[0].item(\"txtMyTextBox\").focus()");
    sb.Append("</script>");

    if (!IsStartupScriptRegistered("setFocus"))
    {
    RegisterStartupScript("setFocus", sb.ToString());
    }


    This assumes that the TextBox controls is called txtMyTextBox.

    Jim Cheshire, MCSE, MCSD [MSFT]
    Developer Support
    ASP.NET


    This post is provided as-is with no warranties and confers no rights.

    --------------------
    >From: Guinness Mann <>
    >Subject: Setting focus on TextBox
    >Date: Wed, 3 Dec 2003 16:14:46 -0700
    >Message-ID: <>
    >Organization: Old Dublin Brewery
    >X-Newsreader: MicroPlanet Gravity v2.60
    >Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    >NNTP-Posting-Host: 128.196.21.152
    >Lines: 1
    >Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
    >Xref: cpmsftngxa06.phx.gbl

    microsoft.public.dotnet.framework.aspnet.webcontrols:16624
    >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    >
    >Greetings,
    >
    >I have a textbox in my aspx file:
    >
    ><INPUT id="txtNotes" runat="server" style="Z-INDEX: 103; LEFT: 0px;
    > WIDTH: 749px; POSITION: absolute; TOP: 0px; HEIGHT: 192px"
    > type="text" size="119" value="Error Notes">
    >
    >and a link in my aspx.cs file:
    >
    >protected HtmlInputText txtNotes;
    >
    >Normally, txtNotes is invisible, but at certain points in the program I
    >want to set Visible to true and set focus on the control.
    >
    >I can make everything work except for the part about setting focus. Is
    >there anything I can do from the server side to set focus to my text
    >control in response to a button press?
    >
    >-- Rick
    >
    >
    Jim Cheshire [MSFT], Dec 3, 2003
    #2
    1. Advertising

  3. In article <>,
    says...
    > Yes. You will need to use RegisterStartupScript to add a client-side
    > script to set the focus. Here's an example:
    >
    > System.Text.StringBuilder sb = new System.Text.StringBuilder("");
    >
    > sb.Append("<script language=\"JavaScript\">");
    > sb.Append(" document.forms[0].item(\"txtMyTextBox\").focus()");
    > sb.Append("</script>");
    >
    > if (!IsStartupScriptRegistered("setFocus"))
    > {
    > RegisterStartupScript("setFocus", sb.ToString());
    > }


    Thanks, Jim. I found another workaround that also involved a client-
    side script, but your solution intrigues me -- I didn't know
    "RegisterStartupScript" existed. I'll be chasing it down in the
    documentation.

    -- Rick
    Guinness Mann, Dec 4, 2003
    #3
  4. Rick,

    No problem. One thing I'll point out is that many ASP.NET developers don't
    truly understand RegisterStartupScript and how it differs from
    RegisterClientScriptBlock. At the risk of spouting too much information,
    I'll give you a brief bit of info on it.

    Both of these methods rely on the fact that the browser will execute script
    when it is encountered unless that script is inside of a function.
    Therefore, the difference between RegisterStartupScript and
    RegisterClientScriptBlock is the positioning of the script registered with
    them. If you use RegisterStartupScript, the script is placed immediately
    after the closing </form> tag on the page. If you use
    RegisterClientScriptBlock, the script is placed prior to the opening <form>
    tag. Therefore, RegisterStartupScript should always be used when you need
    to interact with some element on your form. If you tried to interact with
    a control on your form with RegisterClientScriptBlock, you would find that
    the element would not yet exist.

    The exception to that is when you use RegisterClientScriptBlock to write
    out a function and then explicitly call that function on a button click,
    onLoad attribute of the <body>, etc. While that will certainly work, it is
    often not as efficient as simply using RegisterStartupScript.

    Hope that helps.

    Jim Cheshire, MCSE, MCSD [MSFT]
    Developer Support
    ASP.NET


    This post is provided as-is with no warranties and confers no rights.

    --------------------
    >From: Guinness Mann <>
    >Subject: RE: Setting focus on TextBox
    >Date: Thu, 4 Dec 2003 08:59:09 -0700
    >Message-ID: <>
    >References: <>

    <>
    >Organization: Old Dublin Brewery
    >X-Newsreader: MicroPlanet Gravity v2.60
    >Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    >NNTP-Posting-Host: 128.196.21.152
    >Lines: 1
    >Path:

    cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
    ..phx.gbl!tk2msftngp13.phx.gbl
    >Xref: cpmsftngxa07.phx.gbl

    microsoft.public.dotnet.framework.aspnet.webcontrols:16572
    >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    >
    >In article <>,
    > says...
    >> Yes. You will need to use RegisterStartupScript to add a client-side
    >> script to set the focus. Here's an example:
    >>
    >> System.Text.StringBuilder sb = new System.Text.StringBuilder("");
    >>
    >> sb.Append("<script language=\"JavaScript\">");
    >> sb.Append(" document.forms[0].item(\"txtMyTextBox\").focus()");
    >> sb.Append("</script>");
    >>
    >> if (!IsStartupScriptRegistered("setFocus"))
    >> {
    >> RegisterStartupScript("setFocus", sb.ToString());
    >> }

    >
    >Thanks, Jim. I found another workaround that also involved a client-
    >side script, but your solution intrigues me -- I didn't know
    >"RegisterStartupScript" existed. I'll be chasing it down in the
    >documentation.
    >
    >-- Rick
    >
    Jim Cheshire [MSFT], Dec 4, 2003
    #4
  5. In article <>,
    says...
    > No problem. One thing I'll point out is that many ASP.NET developers don't
    > truly understand RegisterStartupScript and how it differs from
    > RegisterClientScriptBlock. At the risk of spouting too much information,
    > I'll give you a brief bit of info on it.


    Ok, I think I've got it now. My workaround was to edit the aspx file
    and add the script at the bottom -- essentially the same thing the
    RegisterStartupScript does, I think.

    I'm curious as to why RegisterStartupScript exists, as opposed to
    editing the aspx file?

    -- Rick
    Guinness Mann, Dec 9, 2003
    #5
  6. Rick,

    The reason for RegisterStartupScript and RegisterClientScriptBlock is the
    same. In cases where your client-side script relies on something
    determined during server-side processing, you cannot add script directly
    into the ASPX page. Legacy ASP developers would write script like this:

    <script language="JavaScript">
    function someFunction() {
    alert('<%=some_variable_from_server_code%>');
    }
    </script>

    In ASP.NET, this type of inline code is discouraged because it makes code
    hard to debug. Therefore, this same developer can use
    RegisterClientScriptBlock and generate the script.

    Jim Cheshire, MCSE, MCSD [MSFT]
    Developer Support
    ASP.NET


    This post is provided as-is with no warranties and confers no rights.

    --------------------
    >From: Guinness Mann <>
    >Subject: RE: Setting focus on TextBox
    >Date: Tue, 9 Dec 2003 13:35:46 -0700
    >Message-ID: <>
    >References: <>

    <>
    <>
    <>
    >Organization: Old Dublin Brewery
    >X-Newsreader: MicroPlanet Gravity v2.60
    >Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    >NNTP-Posting-Host: 128.196.21.152
    >Lines: 1
    >Path:

    cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.
    phx.gbl
    >Xref: cpmsftngxa07.phx.gbl

    microsoft.public.dotnet.framework.aspnet.webcontrols:16707
    >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    >
    >In article <>,
    > says...
    >> No problem. One thing I'll point out is that many ASP.NET developers

    don't
    >> truly understand RegisterStartupScript and how it differs from
    >> RegisterClientScriptBlock. At the risk of spouting too much

    information,
    >> I'll give you a brief bit of info on it.

    >
    >Ok, I think I've got it now. My workaround was to edit the aspx file
    >and add the script at the bottom -- essentially the same thing the
    >RegisterStartupScript does, I think.
    >
    >I'm curious as to why RegisterStartupScript exists, as opposed to
    >editing the aspx file?
    >
    >-- Rick
    >
    Jim Cheshire [MSFT], Dec 10, 2003
    #6
  7. In article <vtK#>,
    says...
    > The reason for RegisterStartupScript and RegisterClientScriptBlock is the
    > same. In cases where your client-side script relies on something
    > determined during server-side processing, you cannot add script directly
    > into the ASPX page. Legacy ASP developers would write script like this:
    >
    > <script language="JavaScript">
    > function someFunction() {
    > alert('<%=some_variable_from_server_code%>');
    > }
    > </script>
    >
    > In ASP.NET, this type of inline code is discouraged because it makes code
    > hard to debug. Therefore, this same developer can use
    > RegisterClientScriptBlock and generate the script.


    The other good reason I've found is that if you're rendering the page
    and (as you point out) the client side script output relies on something
    determined during server-side processing, and if that determination
    could come from more than one place in the server side code, then
    RegisterClientScriptBlock will ensure that the script block only gets
    included once.

    --Rick
    Guinness Mann, Dec 15, 2003
    #7
    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. George Durzi

    Setting Focus on an asp:textbox

    George Durzi, Aug 20, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    571
    Marina
    Aug 20, 2003
  2. test test

    setting focus to a textbox

    test test, Aug 30, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,737
    Kevin Spencer
    Aug 30, 2003
  3. Jason
    Replies:
    4
    Views:
    1,593
    Jason
    May 7, 2007
  4. Replies:
    2
    Views:
    326
    Thomas 'PointedEars' Lahn
    Jun 6, 2005
  5. Roger
    Replies:
    3
    Views:
    307
Loading...

Share This Page