访问JavaScript变量在cs文件的价值是在code后面? [英] Access javascript variable's value in .cs file that is in in code behind?

查看:82
本文介绍了访问JavaScript变量在cs文件的价值是在code后面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要访问Java脚本的返回值,这是一个变量,一个标签,我想在cs文件中使用它,我该怎么办呢?我取了一个变量,也是asp.net的标签控制放的JavaScript ...我得到的值既办法,但需要它cs文件将其保存在我的数据bsae,请帮助我。谢谢U

 < SCRIPT LANGUAGE =&Java脚本GT;
    功能setup_ip(JSON){
        VAR htmlx =您的IP地址:< B>中+ json.IP;
        htmlx + =< / B> |国家:+ json.countryName;
        如果(json.cityName!=未知|| json.regionName!=未知){
            htmlx + =|城市:+ json.cityName +/+ json.regionName;
            变种S =|城市:+ json.cityName +/+ json.regionName;
        }其他{
            htmlx + =|你的时间:+ json.localTimeZone;
        }        $(#myipx)HTML(htmlx)。
     // $('#<%= lbl1.ClientID%GT;')文本(htmlx)。
        $('#<%= lbl1.ClientID%GT;')文本(S)。       // VAR txtCountry =的document.getElementById('<%= lbl1.ClientID%GT;');    }    $(文件)。就绪(函数(){
        EasyjQuery_Get_IP(setup_ip,全);    });
    < / SCRIPT>
< /头>
<身体GT;
    <表ID =form1的=服务器>
    < D​​IV>
    < D​​IV CLASS =内部>
< p =类欢迎辞>
<一href=\"http://www.easyjquery.com/detect-get-clients-ip-address-country-using-javascript-php/\"id=\"topid\"标题=的JavaScript,PHP jQuery的API检测客户端的IP地址和国家 - 地理位置>
< ASP:标签ID =myipx=服务器的ClientIDMode =静态>< / A>< / ASP:标签>检测客户端的IP地址 - 国家 - 城市< / SPAN>
< BR />
< ASP:标签ID =LBL1=服务器的ClientIDMode =静态>< / ASP:标签>
                &所述; / P>
                 < ASP:文本框ID =txtCountry=服务器>< / ASP:文本框>
            <! - #END页脚纹理 - >
            < / DIV>    < / DIV>
    < /表及GT;
< /身体GT;


解决方案

要使用这个值,你有以下选择:

1)由于标签是你的服务器端控件,你可以轻松地访问标签文本价值的.cs文件,以便主要问题与JavaScript变量在这里你可以指定这个值到ASP:隐藏字段,以便您还可以访问此在的.cs文件,如:

 < SCRIPT LANGUAGE =&Java脚本GT;
功能setup_ip(JSON){$(#HDN)VAL(你的JavaScript变量)。
}
< / SCRIPT>
 < ASP:的ScriptManager ID =ScriptManager1=服务器的EnablePageMethods =真ScriptMode =释放>
< / ASP:ScriptManager的>
< ASP:的UpdatePanel ID =UpnlHidden=服务器>
    <&的ContentTemplate GT;
        <输入=服务器类型=隐藏ID =HDN值=0/>
    < /&的ContentTemplate GT;
< / ASP:的UpdatePanel>

CS文件:

 字符串str = hdn.value //你在哪里都希望存储的价值

2),您可以在这里使用PageMethod的,并通过这两个值作为参数的另一个选项.cs文件。

JavaScript的:

  PageMethods.getValue(参数1,参数,OnSucceeded,OnFailed);
功能OnSucceededID(结果,方法名)
{
   //得到结果返回,如果你想PageMethod的对服务器端成功后执行
} 功能OnFailedID(错误,方法名){
        如果(错误!== NULL){
           警报(error.get_message());
        }
    }

CS文件:

  [的WebMethod(enableSession:真)
    公共静态的返回类型的getValue(参数类型参数1,参数类型参数2)
    {
        你想使用这些值做什么。    }

希望它可以帮助你。

I want to access java script's return value , which in is in a variable and a label, i want to use it in .cs file, how do i do that? I have fetch the out put of javascript in a variable and also a label control of asp.net... I get the values both the ways, but in need it .cs file to save it in my data bsae, help me please.. Thank u

<script language=Javascript>
    function setup_ip(json) {
        var htmlx = " Your IP Address: <b>" + json.IP;
        htmlx += "</b> | Country: " + json.countryName;
        if (json.cityName != "Unknown" || json.regionName != "Unknown") {
            htmlx += " | City: " + json.cityName + " / " + json.regionName;
            var s = " | City: " + json.cityName + " / " + json.regionName;
        } else {
            htmlx += " | Your Time: " + json.localTimeZone;
        }

        $("#myipx").html(htmlx);
     //   $('#<%= lbl1.ClientID %>').text(htmlx);
        $('#<%= lbl1.ClientID %>').text(s);

       // var txtCountry = document.getElementById('<%=lbl1.ClientID %>');        

    }

    $(document).ready(function() {


        EasyjQuery_Get_IP("setup_ip", "full");

    });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div class="inner">
<p class="welcome-message">
<a href="http://www.easyjquery.com/detect-get-clients-ip-address-country-using-javascript-php/"id="topid" title="Javascript, PHP jQuery API Detect Clients IP Address and Country - Geo Location"   >
<asp:Label ID="myipx" runat="server"  ClientIDMode="Static"></a></asp:Label>Detecting Clients IP Address - Country - City</span>
<br />
<asp:Label ID="lbl1" runat="server"  ClientIDMode="Static"></asp:Label>
                </p>
                 <asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>


            <!-- END #footer-texture -->
            </div>

    </div>
    </form>
</body>

解决方案

To use this values you have following options :

1.)As label is your server side control you can easily access label text value in .cs file so main issue with javascript variable here you can assign this value to asp:hidden field so you can also access this in .cs file like :

<script language=Javascript>
function setup_ip(json) {

$("#hdn").val('your javascript variable');
}
</script>
 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" ScriptMode="Release">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpnlHidden" runat="server">
    <ContentTemplate>
        <input runat="server" type="hidden" id="hdn" value="0" />
    </ContentTemplate>
</asp:UpdatePanel>

cs file :

  string str = hdn.value//Where ever you want to store value

2.)Another option you can use pagemethod here and pass both values as parameter to .cs file.

javascript :

  PageMethods.getValue(parameter1,parameter2, OnSucceeded, OnFailed);
function OnSucceededID(result, methodName)
{
   //get result back if you want after successfully execution of pagemethod on server side  
}

 function OnFailedID(error, methodName) {
        if (error !== null) {
           alert(error.get_message());
        }
    }

CS file :

   [WebMethod(enableSession: true)]
    public static returntype getValue(parameter type parameter1,parameter type parameter2)
    {
        do what you want to do with these values.

    }

Hope it helps you.

这篇关于访问JavaScript变量在cs文件的价值是在code后面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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