Masterpages and Javascript


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

Advertisements

?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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

dgk

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

Advertisements


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

Top