views:

38

answers:

2

In a section in the <head> tags of my document I have code that lets me use the Id of my asp.net control from JavaScript like this:

<script language="javascript" type="text/javascript">
var customerId = '<%= Me.CustomerTextbox.ClientID %>';
</script>

However, if I want to modify the page structure in later stages of the page life cycle, it give me an error because I have the ASP tags in there. A work-around is enclosing the ASP tags in a server control like this:

<div id="customerIdContainer" runat="server">
<script language="javascript" type="text/javascript">
var customerId = '<%= Me.CustomerTextbox.ClientID %>';
</script>
</div>

But then I get a warning that a div tag is not allowed in the head tag. So is there a container tag that is valid in the head that I can add runat="server" to to make it a server control so I can workaround this problem without the warnings?

A: 

Move the script tag outside the head section. ;-)

meep
+1  A: 

Some thoughts:

  • Can you just make "head" into "runat=sever"?
  • If you use <asp:placeHolder id="whatever" runat="server" /> inside the head tag, will that complain? If not, you can add controls to the placeholder from the code.
  • Scripts can exist outside of the head tags, so just move it.
John Fisher