Setting Focus on an asp:textbox

  • Thread starter Thread starter George Durzi
  • Start date Start date
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?
 
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.
 
Yes, and it's rendering at the end as defined by the RegisterStartupScript
method. In my first message, I posted
 
It worked. Very strange. the only think I changed was removing
sb.Append(@"<!--");
and
sb.Append(@"-->");
 
Back
Top