F.P
F.P

Reputation: 17831

Setting a JScript variable to contentplaceholder

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 :)

Upvotes: 0

Views: 258

Answers (2)

Oded
Oded

Reputation: 499002

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!";

Upvotes: 0

LJW
LJW

Reputation: 2388

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.

Upvotes: 1

Related Questions