views:

35

answers:

3

in aspx code behind, define a var like:

Public rate as decimal;

then in page markup, put a control like:

<asp:HiddenField ID="myRate" runat="server" Value='<%=rate%>'/>

then in javascript try to test this value:

alert(document.getElementById('<%=myRate.ClientID%>').value);   

it gave me the value as <%=myRate%>, not something like 0.01 in alert popup.

How to resolve this problem

A: 

try setting the value in the page load in the code behind,

I am suprised this approach worked as I didnt think you could use the response.write construct in a server control?

Another option may be to set it via the binding syntax so

<asp:HiddenField ID="myRate" runat="server" Value='<%#this.rate%>'/>
Pharabus
+5  A: 

Server tags aren't evaluated in this case (which is why you see the literal text):

<asp:HiddenField ID="myRate" runat="server" Value='<%=rate%>'/>

The easiest options are to set it in the code-behind:

myRate.Value = rate;

or, unless it needs to be a server control, just use a hidden input:

<input id="myRate" type="hidden" value="<%=rate%>" />

And get it using that ID:

document.getElementById('myRate').value
Nick Craver
Great! your solution is working fine. Thank you very much.
KentZhou
A: 

How I would do it

<asp:HiddenField ID="MyRate" runat="server" Value="<%# this.rate %>" />

Then in my javascript:

alert(document.getElementById('<%=myRate.ClientID%>').value);
Anthony