Setting focus on TextBox

G

Guinness Mann

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
 
J

Jim Cheshire [MSFT]

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
(e-mail address removed)

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

--------------------
 
G

Guinness Mann

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
 
J

Jim Cheshire [MSFT]

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
(e-mail address removed)

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

--------------------
From: Guinness Mann <[email protected]>
Subject: RE: Setting focus on TextBox
Date: Thu, 4 Dec 2003 08:59:09 -0700
Message-ID: <[email protected]>
References: <[email protected]>
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

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
 
G

Guinness Mann

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
 
J

Jim Cheshire [MSFT]

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
(e-mail address removed)

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

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

Guinness Mann

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top