如何从javascript设置C#变量值

时间:2021-06-15 23:20:56

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

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

1 个解决方案

#1


5  

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

如果asp.net HidenField webControl有一个值,那么您需要做的就是以下内容:

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:

如果要在分配值时处理onValueChanged事件,请执行以下操作:

        <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.

虽然您可以使用在C#中使用javascript设置的asp.net HiddenField的值,但请确保您了解这只能在回发后执行。

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

以下是有关客户端/服务器关系的一些信息。你的问题分别是Javascript和C#。

#1


5  

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

如果asp.net HidenField webControl有一个值,那么您需要做的就是以下内容:

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:

如果要在分配值时处理onValueChanged事件,请执行以下操作:

        <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.

虽然您可以使用在C#中使用javascript设置的asp.net HiddenField的值,但请确保您了解这只能在回发后执行。

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

以下是有关客户端/服务器关系的一些信息。你的问题分别是Javascript和C#。