如何在c#Code后面的变量中分配Javascript返回值 [英] How to assign Javascript return value in c# Code behind Variable

查看:59
本文介绍了如何在c#Code后面的变量中分配Javascript返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Javascript:

< script>

函数GetValue(){

返回10;

}

< / script>



C#.NEt



protected void Page_Load(object sender,EventArgs e)

{

int data

ClientScript .RegisterStartupScript(this.GetType(),GetData,a = GetValue(); alert(a),true);

数据= a; // a是Javascript变量

}



在C#页面加载方法中如何将Javascipt返回值设置为C#变量。

推荐答案

您的JavaScript代码仅在客户端站点上的Web浏览器中执行。并且您只能在服务器端执行C#代码,仅响应某些HTTP请求;这两部分代码从不相互调用。对?对!



结论?您需要通过JavaScript在客户端生成HTTP请求,并按照您喜欢的方式在服务器端处理它。没有别的办法。原则上,HTTP请求是以HTML格式生成的,但在这种情况下,可能唯一适当的技术是使用Ajax。请参阅:

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

http://www.w3schools.com/ajax/default.asp [< a href =http://www.w3schools.com/ajax/default.asptarget =_ blanktitle =New Window> ^ ]。



在Web项目中,特别是在ASP.NET中,有不同的方法和框架用于利用Ajax。你可以考虑所有,但我宁愿建议一个:使用jQuery。请参阅:

http://en.wikipedia.org/wiki/JQuery [< a href =http://en.wikipedia.org/wiki/JQuerytarget =_ blanktitle =New Window> ^ ],

http://jquery.com/ [ ^ ]。



以下是通过jQuery使用Ajax的方法:http://api.jquery.com/category/ajax/ [ ^ ]。



如果您需要学习jQuery(我强烈推荐),请从这里开始:

http://docs.jquery.com/Tutorials:How_jQuery_Works [ ^ ],

http://docs.jquery.com/Tutorials [<一个href =http://docs.jquery.com/Tutorialstarget =_ blanktitle =New Window> ^ ]。



新年快乐!



-SA
Your JavaScript code is executed in a Web browser, only on the client site. And you C# code behind is executed only on server side, only in response to some HTTP request; these two parts of code never call one another. Right? Right!

Conclusions? You need to generate an HTTP request on client side via your JavaScript and process it on server side the way you like. There won''t be any other way. In principle, the HTTP requests are generated be HTML forms, but in this case, probably the only adequate technique would be using Ajax. Please see:
http://en.wikipedia.org/wiki/Ajax_%28programming%29[^],
http://www.w3schools.com/ajax/default.asp[^].

There are different ways and frameworks used to leverage Ajax in your Web project, and in ASP.NET in particular. You can consider then all, but I would rather advice one: using jQuery. Please see:
http://en.wikipedia.org/wiki/JQuery[^],
http://jquery.com/[^].

Here is how you can use Ajax via jQuery: http://api.jquery.com/category/ajax/[^].

If you need to learn jQuery (which I would highly recommend), please start here:
http://docs.jquery.com/Tutorials:How_jQuery_Works[^],
http://docs.jquery.com/Tutorials[^].

Happy New Year!

—SA


Hi


试试这个,







Hi
Try this,



//inside head tag

<script type="text/javascript">
        function test() {
            if (confirm("test")) {
                document.getElementById('<%=hdnTestValue.ClientID %>').value = "true";
            }
            else {
                document.getElementById('<%= hdnTestValue.ClientID %>').value = "false";
            }
        }
    </script>













<!-- Body Tag -->

    <form id="form1" runat="server">
    <div>
    <asp:button runat="server" id="Button1" onclientclick="test()" xmlns:asp="#unknown">
            onclick="Button1_Click" Text="Submit" />



    <asp:hiddenfield runat="server" id="hdnTestValue" />


 <asp:label id="lblRvalue" runat="server"></asp:label>




    </asp:button></div>
    </form>
















//Code behind



  protected void Button1_Click(object sender, EventArgs e)
    {
        lblRvalue.Text = hdnTestValue.Value;
    }









将您的值分配给隐藏字段使用JavaScript并将隐藏字段值传递给服务器端。







如果找到有帮助,请标记为答案..



快乐编码...





Assign your value to hidden field with JavaScript and pass hidden field value to server side.



Please Mark as Answer if find helpful..

Happy coding...


在你的aspx页面中添加一个隐藏字段:

Add a hidden field in your aspx page as:
<asp:HiddenField ID="myHiddenField" runat="server" />



在JavaScript函数中使用:


In your JavaScript function use:

<script>
function GetValue() {
document.getElementById('myHiddenField').value = "10";
}
</script>





在你的pageload活动中:



In your pageload event:

int Data = Convert.ToInt32(myHiddenField.Value);


这篇关于如何在c#Code后面的变量中分配Javascript返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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