views:

84

answers:

2

I have a content page in my ASP.NET-page and want to set a JScript variable to it programatically, so I can use it within the JScript sections of the rendered page afterwards.

How can I start with this problem? I have a "head"-content that fills a ContentPlaceHolder in a master page, but have no idea how to write new content into it.

I know there are some functions that deal with Page.Controls but I really didn't anything with what I can easily add some content to the... content...

I hope someone understands my question and has some advice :)

+1  A: 

Try:

<asp:Content runat="server" contentplaceholderid="head">

  <script type="text/javascript">
     var myVariable = '<%= MyDotNetProperty %>';
  </script>

</asp:Content>

myVariable will then be available in all JS for that page.

Marcus
+1 I often use something like var controlID = <%=ControlName.clientID%> to get the verbose id that ASP.NET generates
Chris Ballance
A: 

You can output the control client id (which is the ID that will be given to it on the browser) to javascript and use that to output more things to the control:

var elem = document.getElementById("<%= myControl.ClientId %>");
elem.innerHtml = "Look, no hands!";
Oded