如何设置从JavaScript C#的变量值 [英] How to set a C# variable value from javascript
问题描述
我在做一个Ajax请求和存储我在隐藏field.I'm响应使用getelementbyid.value.This javascript函数是身体上通过JavaScript这样做onload.Now后,我得到这个值,我想用这在C#。我不能有任何按钮的onclick事件sort.Just的或任何有一个隐藏的输入类型
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
推荐答案
如果一个asp.net HidenField的WebControl的值那么所有你需要做的是以下内容:
If an asp.net HidenField webControl has a value then all you need to do is the following:
aspx页面:
<asp:hiddenfield id="hf_MyValue"
value="whatever"
runat="server"/>
CS页:
string value = hf_MyValue.Value;
如果你想要做的东西时,它的分配处理onValueChanged事件的值:
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"/>
虽然可以使用正在使用的JavaScript在C#中设置一个asp.net HiddenField的值,请确保你明白,这只能在回发之后进行。
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.
下面是一些信息的客户端/服务器关系。使用Javascript和C在你的问题分别为#。
Here is some info on the Client/Server relationship. Javascript and C# respectively in your question.
这篇关于如何设置从JavaScript C#的变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!