Hi,
I'm using the OnPreRender method within a Server Control to generate some client side JavaScript, which is to bring focus to a textbox. The method is as follows:
Code:
protected override void OnPreRender(EventArgs e)
{
Page.ClientScript.RegisterStartupScript(typeof(Page),
"ControlFocus", "document.getElementById('" + this.ClientID +
"').focus();", true);
}
When the web page is displayed, the page source (relevant bits) is as follows:
Code:
<html>
....
<div>
<span id="WebCustomControl1_1" style="display:inline-block;background-
color:Green;font-weight:bold;height:28px;width:152px;">
<div>
<input type="text" id="WebCustomControl1_1" name="WebCustomControl1_1"
value="Hello Skin" id="WebCustomControl1_1" style="display:inline-block;
background-color:Green;font-weight:bold;height:28px;width:152px;" />
</div>
</span>
</div>
<script type="text/javascript">
//<![CDATA[
document.getElementById('WebCustomControl1_1').focus();//]]>
</script>
...
</html>
The focus does not go to the textbox.
Can anyone help with this? I'm working through a textbook example (Wrox C# Pro Ch.23) and there's nothing on the erratum page. If anyone has some of their own examples they'd like to share, that would be appreciated.