最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览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.

发布评论

评论列表(0)

  1. 暂无评论