Setting Focus on an asp:textbox

G

George Durzi

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?
 
G

George Durzi

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.
 
G

George Durzi

Yes, and it's rendering at the end as defined by the RegisterStartupScript
method. In my first message, I posted
 
G

George Durzi

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

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top