简体   繁体   中英

How to set a C# variable value from javascript

我正在制作一个ajax请求并将我的响应存储在一个隐藏的字段中。我是通过javascript使用getelementbyid.value执行此操作。这个javascript函数在body onload.Now我得到这个值之后我想在C#中使用它。我不能有任何按钮onclick事件或任何类型的东西。只有一个隐藏的输入类型

If an asp.net 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.net 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM