How to set a C# variable value from javascript - Stack Overflow

admin2025-04-17  0

I'm making an ajax request and storing my response in an hidden field.I'm doing this through javascript using getelementbyid.value.This javascript function is on body onload.Now after I get this value I would like to use this in C#.I can't have any button onclick event or anything of that sort.Just have a hidden input type

I'm making an ajax request and storing my response in an hidden field.I'm doing this through javascript using getelementbyid.value.This javascript function is on body onload.Now after I get this value I would like to use this in C#.I can't have any button onclick event or anything of that sort.Just have a hidden input type

Share Improve this question asked Nov 12, 2010 at 5:25 gizgokgizgok 7,66923 gold badges87 silver badges128 bronze badges 1
  • As per my understanding u cant use script variable on server side. Also can u put some piece of code here? – Amit Soni Commented Nov 12, 2010 at 5:27
Add a ment  | 

1 Answer 1

Reset to default 5

If an asp HidenField webControl has a value then all you need to do is the following:

aspx page:

        <asp:hiddenfield id="hf_MyValue"
          value="whatever" 
          runat="server"/>

cs page:

string value = hf_MyValue.Value;

If you want to do something with the value when it's assigned handle the onValueChanged event:

        <asp:hiddenfield id="hf_MyValue"
          onvaluechanged="ValueHiddenField_ValueChanged"
          value="whatever" 
          runat="server"/>

While you CAN use a value of an asp HiddenField that is set using javascript in C# make sure that you understand that this can only be done after a postback.

Here is some info on the Client/Server relationship. Javascript and C# respectively in your question.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744885743a272524.html

最新回复(0)