如何在C#中从javascript到codebehind获取值 [英] How to get the values from javascript to codebehind in C#

查看:72
本文介绍了如何在C#中从javascript到codebehind获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在做一个电子商务项目。我正在使用java脚本获取用户国家和系统的IP地址。我已经在标签中使用了这些值。标签中的值我必须使用asp控件来更新货币。但是当我运行代码时它表示innerHTML中没有值。我必须从java脚本中获取标签值并在后面的代码中使用它。我怎么能超过来吧。请帮帮我。我的示例代码如下。

 <   script     src   =   http://code.jquery.com/jquery-1.8.2.js   类型  =  text / javascript >  <   / script  >  
< script 类型 = text / javascript > ;
var strip,strcountry,strcity,strregion,strlatitude,strlongitude,strtimezone
function GetUserInfo(data){
strip = data.host; strcountry = data.countryName; strcity = data.city;
strregion = data.region; strlatitude = data.latitude; strlongitude = data.longitude;
strtimezone = data.timezone;
}
$(function(){
BindUserInfo();
})
函数BindUserInfo(){
document.getElementById('lblIP') .innerHTML = strip;
document.getElementById('lblCountry')。innerHTML = strcountry;
< / script >

< script type = text / javascript src = < span class =code-keyword> http://smart-ip.net/geoip-json?callback=GetUserInfo > < / script >
< span class =code-keyword>< / head >

< body &g t;
< div >
< table id = tbDetails cellpadding = 2 cellspacing = 2 style = border:1px solid#000;< br mode = hold = < span class =code-keyword> /> font-family:Verdana; >
< tr style = background-color:#DC5807;白颜色; font-weight:bold >
< td colspan = 2 align = center >
用户信息
< / td >
< / tr >
< tr style = border:solid 1px#000000 >
< td align = right >
IP:
< / td >
< td >
< label id = lb lIP / >
< / td >
< / tr > ; < / table > < / div >

解决方案

(function(){
BindUserInfo();
})
函数BindUserInfo(){
document.getElementById('lblIP')。innerHTML = strip;
document.getElementById('lblCountry')。innerHTML = strcountry;
< / script >

< script type = text / javascript src = < span class =code-keyword> http://smart-ip.net/geoip-json?callback=GetUserInfo > < / script >
< span class =code-keyword>< / head >

< body &g t;
< div >
< table id = tbDetails cellpadding = 2 cellspacing = 2 style = border:1px solid#000;< br mode = hold = < span class =code-keyword> /> font-family:Verdana; >
< tr style = background-color:#DC5807;白颜色; font-weight:bold >
< td colspan = 2 align = center >
用户信息
< / td >
< / tr >
< tr style = border:solid 1px#000000 >
< td align = right >
IP:
< / td >
< td >
< label id = lb lIP / >
< / td >
< / tr > ; < / table > < / div >


HI,





使用 innerText 在javascript中为标签控件赋值

  document  .getElementById('  lblIP')。innerText =   some; 





在asp.net中你无法获得代码背后的标签控制值在客户端更新(即javascript)



简单的方法是你可以使用隐藏字段将值存储在javascript中,你可以在后面的代码中读取相同内容。





示例:



每当你给标签赋值时,也要更新相应的隐藏字段

  document  .getElementById('  lblIP')。innerText =   some; 
document .getElementById(' hdn_lblIP').value = some;





在您的页面中创建隐藏控件..

 <   asp:HiddenField     ID  < span class =code-keyword> =  hdn_lblIP    runat   =  server    /  >  




服务器中的
(代码隐藏)你可以这样阅读



 取值tring  ipvalue = hdn_lblIP.Value; 





希望你理解。


访问这里.....





http://www.aspdotnet-suresh.com/2013/10/javascript-get-aspnet-textbox-value-label-value.html [ ^ ]

Hello every one,
I am doing a e-commerce project.In that i am using java script for getting users country and system's IP address.I have taken those values in labels.using that value from the label i have to update currency using asp controls.But when i run the code it is saying there is no value in innerHTML.I have to get label value from java script and have use it in code behind.How can i over come this.Please help me.My sample code is below.

<script src="http://code.jquery.com/jquery-1.8.2.js" type="text/javascript"></script>
    <script type="text/javascript">
        var strip, strcountry, strcity, strregion, strlatitude, strlongitude, strtimezone
        function GetUserInfo(data) {
            strip = data.host; strcountry = data.countryName; strcity = data.city;
            strregion = data.region; strlatitude = data.latitude; strlongitude = data.longitude;
            strtimezone = data.timezone;
        }
        $(function () {
            BindUserInfo();
        })
        function BindUserInfo() {
            document.getElementById('lblIP').innerHTML = strip;
            document.getElementById('lblCountry').innerHTML = strcountry;
</script>

    <script type="text/javascript" src="http://smart-ip.net/geoip-json?callback=GetUserInfo"></script>
</head>

<body>
    <div>
        <table id="tbDetails" cellpadding="2" cellspacing="2" style="border: 1px solid #000;<br mode=" hold=" />            font-family: Verdana;">
            <tr style="background-color: #DC5807; color: White; font-weight: bold">
                <td colspan="2" align="center">
                    User Information
                </td>
            </tr>
            <tr style="border: solid 1px #000000">
                <td align="right">
                    IP:
                </td>
                <td>
                    <label id="lblIP" />
                </td>
            </tr></table></div>

解决方案

(function () { BindUserInfo(); }) function BindUserInfo() { document.getElementById('lblIP').innerHTML = strip; document.getElementById('lblCountry').innerHTML = strcountry; </script> <script type="text/javascript" src="http://smart-ip.net/geoip-json?callback=GetUserInfo"></script> </head> <body> <div> <table id="tbDetails" cellpadding="2" cellspacing="2" style="border: 1px solid #000;<br mode=" hold=" /> font-family: Verdana;"> <tr style="background-color: #DC5807; color: White; font-weight: bold"> <td colspan="2" align="center"> User Information </td> </tr> <tr style="border: solid 1px #000000"> <td align="right"> IP: </td> <td> <label id="lblIP" /> </td> </tr></table></div>


HI,


use innerText to assign values to the label control in javascript

document.getElementById('lblIP').innerText = "some";



in asp.net you cannot get the values of label control in code behind which is updated in the client side ( ie javascript )

simple way is you can use hidden field to store the values in the javascript and you can read the same in the code behind.


example:

whenever u r assigning a value to the label, update the corresponding hidden field as well

document.getElementById('lblIP').innerText = "some";
            document.getElementById('hdn_lblIP').value = "some";



create a hidden control in your page..

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



in the server ( code behind ) you can read like this

string ipvalue =  hdn_lblIP.Value;



hope you understood.


visit here.....


http://www.aspdotnet-suresh.com/2013/10/javascript-get-aspnet-textbox-value-label-value.html[^]


这篇关于如何在C#中从javascript到codebehind获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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