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