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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14603256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:22:02  来源:igfitidea点击:

Passing values from javascript to code behind in ASP.NET

javascriptasp.netjqgrid-asp.net

提问by Out

I have a variable in aspx.cswhen I click the Datagrid particular row;
Then from the javascript, I should get that value and pass into aspx.cs variable.

aspx.cs当我单击 Datagrid 特定行时,我有一个变量;
然后从 javascript 中,我应该获取该值并传递给 aspx.cs 变量。

how to do this?

这该怎么做?

回答by Aristos

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;

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。