简体   繁体   English

将值从javascript传递到ASP.NET中的代码

[英]Passing values from javascript to code behind in ASP.NET

I have a variable in aspx.cs when I click the Datagrid particular row; 我有一个变量aspx.cs当我点击Datagrid的特定行;
Then from the javascript, I should get that value and pass into aspx.cs variable. 然后从javascript,我应该得到该值并传递到aspx.cs变量。

how to do this? 这个怎么做?

Using html controls 使用html控件

First you use a hidden input control as: 首先,您使用隐藏的输入控件:

<input type="hidden" value="" id="SendA" name="SendA" />

Second you add to that control the value you like to send on code behind using javascript as: 其次,使用javascript将您希望在代码后面发送的值添加到该控件:

document.getElementById("SendA").value = "1";

And then on post back you get that value as: 然后在回帖后你得到的价值为:

Request.Form["SendA"]

Using asp.net Controls 使用asp.net控件

The same way if you use asp.net control can be as: 如果使用asp.net控件,同样的方法可以是:

<asp:HiddenField runat="server" ID="SendA" Value="" />
<script>
   document.getElementById("<%=SendA.ClientID%>").value = "1";
</script>

and get it on code behind with SendA.Value; 并使用SendA.Value;获取代码SendA.Value;

And of course you can use ajax calls to send on code behind values, or simple call url with url parameters that return no content. 当然,您可以使用ajax调用来发送值后面的代码,或使用不返回内容的url参数的简单调用url。

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

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