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

=?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.
 

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

Forum statistics

Threads
473,774
Messages
2,569,600
Members
45,181
Latest member
RexGreenwa
Top