views:

234

answers:

3

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.

+5  A: 
// Use <%=, not <%#
document.getElementById('<%= hBookmark.ClientID %>').value = val;
Sean Bright
A: 

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.

Josh Stodola
A: 

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;
}
Christian Toma