将javascript变量的值传递给后面的C#代码 [英] Pass javascript variable's value to C# code behind

查看:65
本文介绍了将javascript变量的值传递给后面的C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找如何将javascript变量的值传递给C#代码,但是没有得到好结果。关键是如何将对象ID分配给变量,然后将其值传递给后面的C#代码。感谢您是否可以分享您的经验。

I have searched around for how to Pass javascript variable's value to C# code behind, but have not got no good result. The key is how to assign an object ID to the variable and then its value can be passed to C# code behind. Appreciate if you can share your experience.

推荐答案

如果没有向服务器发送和发送HTTP请求,则无法在客户端和服务器端之间进行交互。你可以使用Ajax来实现它:

http://en.wikipedia.org/wiki / Ajax_%28programming%29 [ ^ ]。



使用Ajax的一种便捷方法是使用jQuery .Ajax()http://api.jquery.com/jquery.ajax/ [ ^ ]。



-SA
You cannot interact between client side and server side without sending and HTTP request to the server. You can do it using Ajax:
http://en.wikipedia.org/wiki/Ajax_%28programming%29[^].

One convenient way of using Ajax is using jQuery .Ajax(): http://api.jquery.com/jquery.ajax/[^].

—SA


正如谢尔盖所​​说,你不能将javascript变量直接分配给后面的代码,

你可以做的就像是



在客户端创建一个隐藏字段变量



As Sergey said you cannot assign as javascript variable directly to code behind,
what you can do is like

create a hidden field variable in the client side as

<asp:HiddenField ID="hdnfldVariable" runat="server" />





javascript 你可以为隐藏变量分配一些值





and in the javascript you can assign some value to the hidden variable as

<script type="text/javascript">
        var somefunction = function () {
            var hdnfldVariable = document.getElementById('hdnfldVariable');
            hdnfldVariable.value = 'value from javascript';
        }
    </script>





您可以在<$ c中读取隐藏的字段变量$ c>背后的代码 as





and you can read the hidden field variable in the code behind as

string variable = hdnfldVariable.Value;


这篇关于将javascript变量的值传递给后面的C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆