0

Is there any way to access server side asp.net variable in javascript? I want to do something like

function setBookmark(val)
{
    document.getElementById('<%# hBookmark.ClientID %>').value=val;
}

Is it possible in anyway?

*Note*: hBookmark is a server side html hiddent control, I want to use client ID as IDs are changed as the server controls are rendered.

2
  • Note: hBookmark is a server side html hiddent control, I want to use client ID as IDs are changed as the server controls are rendered. Commented Jul 28, 2009 at 14:58
  • This should work fine, are you running into a problem? I'm not entirely sure what you're asking. Commented Jul 28, 2009 at 15:01

3 Answers 3

5
// Use <%=, not <%#
document.getElementById('<%= hBookmark.ClientID %>').value = val;
Sign up to request clarification or add additional context in comments.

Comments

0

It's possible to use scriptlets like this...

document.getElementById('<%= hBookmark.ClientID %>').value = val;

But note that the Javascript can not reside in an external file; it must be in the markup for this to work.

Comments

0

You can set the variable as value for a hidden input

<input type="hidden" value="<%= hBookmark.ClientID %>" id="hBookmarkClientID" />

And then use it from JavaScript

function setBookmark(val)
{
    var hBookmarkClientID=document.getElementById('hBookmarkClientID').value;

    document.getElementById(hBookmarkClientID).value=val;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.