简体   繁体   English

从javascript重置c#变量的值

[英]reset value of c# variable from javascript

I have a variable _count in codebehind and accessing it from aspx page as below 我在代码隐藏中有一个变量_count ,并从aspx页面访问它,如下所示

 var result='<%=_count%>';

Now I want to reset this value from aspx page I tried this but its not working 现在我想从aspx页面重置这个值我尝试了这个,但它不起作用

'<%=_count%>'=0;

You can not reset the value of asp.net server side veraible from javascript until you do postback or sent async call. 不能重置价值asp.net服务器端veraible从JavaScript,直到你postback或发送async调用。 The code is translated by asp.net on server side and generated html/javascript is sent to client (browswer) Javascript can not access asp.net variables directly. 代码由服务器端的asp.net翻译,生成的html/javascript被发送到客户端(浏览器)Javascript无法直接访问asp.net变量。 You can assign changed value to hidden field and access that field on server side on postback. 您可以将更改的值分配给hidden字段,并在回发时访问服务器端的该字段。

Html HTML

<input type="hidden" runat="server" id="hdn" />

Javascript 使用Javascript

document.getElementById('hdn').value = "123";

Code behind 代码背后

_count = int.Parse(hdn.Value);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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