Masterpages and Javascript

  • Thread starter Thread starter dgk
  • Start date Start date
D

dgk

I was working on a simple app and I would use some javascript to set
the focus:

Dim sb As StringBuilder = New StringBuilder
sb.Append("<script language = 'javascript'>")
sb.Append(" document.getElementById('txtWarrant').focus()")
sb.Append("</script>")
ClientScript.RegisterStartupScript(Me.GetType, "focus",
sb.ToString)

Now I've added a Masterpage and the same code generates an error:

Microsoft JScript runtime error: 'document.getElementById(...)' is
null or not an object

which I guess means that the masterpage is causing the script to
execute before txtWarrant is created. Is there a way around this?
 
No, the problem is that the id of the html element no longer is the same
as the id of the server control. When you put server controls in a
container, like a contentplaceholder, there is a prefix added to the id
to ensure that there are no conflicts.

Use the ClientID property of the control to get the id that is created
for the html element.
 
No, the problem is that the id of the html element no longer is the same
as the id of the server control. When you put server controls in a
container, like a contentplaceholder, there is a prefix added to the id
to ensure that there are no conflicts.

Use the ClientID property of the control to get the id that is created
for the html element.

Yes, perfect. Thanks.
 
Back
Top