I am trying to write javascript variable(hdnField) to server side. In javascript code I have assigned the value of "HiddenField" Control and then I want to write this value to server side. Here's my Client side script:
我正在尝试将javascript变量(hdnField)写入服务器端。在javascript代码中,我已经分配了“HiddenField”控件的值,然后我想将此值写入服务器端。这是我的客户端脚本:
<script type="text/javascript">
var hdnField = document.getElementById('<%= hdnField.ClientID%>');
hdnField.value = 100;
</script>
Server side:
<form action="#" runat="server">
<div class="left"><%Response.Write(hdnField.Value);%></div>
<asp:hiddenfield id="hdnField" runat="server" ></asp:hiddenfield>
</form>
I viewed the Page src and was able to retrieve the "hdnField" which is :
我查看了Page src,并且能够检索到“hdnField”,它是:
<input id="hdnField" type="hidden" name="hdnField" value="100 ">
1 个解决方案
#1
0
I don't know where
我不知道在哪里
<div class="left"><%Response.Write(hdnField.Value);%></div>
Comes into play because that looks more ASP than ASP.NET web forms, but if you have:
进入游戏,因为它看起来比ASP.NET Web表单更多的ASP,但如果你有:
<asp:hiddenfield id="hdnField" runat="server" ></asp:hiddenfield>
You can read and write to this on the client via:
您可以通过以下方式在客户端上读取和写入:
document.getElementById('<%= hdnField.ClientID %>').value = 'XYZ';
alert(document.getElementById('<%= hdnField.ClientID %>').value);
On the server, you can read and write to this via:
在服务器上,您可以通过以下方式读取和写入:
hdnField.Text = "XYZ";
var text = hdnField.Text;
As long as hdnField is not in a template or list control, you can refer to it directly on the server, with the <asp:HiddenField> control you can do both. This bridges the gap so that you can change the value on the client, then retrieve the value on postback. AJAX is only necessary if you need to send the value to the server before the next postback occurs, or out of the normal flow of the ASP.NET postback lifecycle.
只要hdnField不在模板或列表控件中,您就可以直接在服务器上引用它,使用
#1
0
I don't know where
我不知道在哪里
<div class="left"><%Response.Write(hdnField.Value);%></div>
Comes into play because that looks more ASP than ASP.NET web forms, but if you have:
进入游戏,因为它看起来比ASP.NET Web表单更多的ASP,但如果你有:
<asp:hiddenfield id="hdnField" runat="server" ></asp:hiddenfield>
You can read and write to this on the client via:
您可以通过以下方式在客户端上读取和写入:
document.getElementById('<%= hdnField.ClientID %>').value = 'XYZ';
alert(document.getElementById('<%= hdnField.ClientID %>').value);
On the server, you can read and write to this via:
在服务器上,您可以通过以下方式读取和写入:
hdnField.Text = "XYZ";
var text = hdnField.Text;
As long as hdnField is not in a template or list control, you can refer to it directly on the server, with the <asp:HiddenField> control you can do both. This bridges the gap so that you can change the value on the client, then retrieve the value on postback. AJAX is only necessary if you need to send the value to the server before the next postback occurs, or out of the normal flow of the ASP.NET postback lifecycle.
只要hdnField不在模板或列表控件中,您就可以直接在服务器上引用它,使用