• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3473
  • Last Modified:

Access Server variable from ASP.NET in javascript

I have a javascript that needs to get the "rootID" value from my asp.net code behind file. The problem is that the link dont write out the id, but it writes out "<%=rootId%>".

It means, this is the link I get:

The link should be like this:

Here is my code:
OnClientClick="try {window.location('add.aspx?id=' + targetEl.id.replace('id','').replace('root_0','<%=rootId%>'));} catch (e) {}"
1 Solution
OnClientClick being a server side function will be parsed before being processed by the server, so that the <%=rootId> had been converted to &lt;=rootId&gt;.
We can get the rootId server varialbe to a javascript variable and use the javascript variable in the OnCLientClick.

var jsRootId = '<%=rootId%>'

OnClientClick="try {window.location('add.aspx?id=' + targetEl.id.replace('id','').replace('root_0',jsRootId));} catch (e) {}"

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now