我想访问java脚本的返回值,它在一个变量和一个标签中,我想在.cs文件中使用它,我该怎么做?我在一个变量中获取了javascript的输出,同时还获取了一个asp.net的标签控件。。。我得到两种方法的值,但需要它.cs文件来保存在我的数据bsae中,请帮助我。。谢谢你
<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>
最佳答案
要使用此值,有以下选项:
1.)由于label是您的服务器端控件,您可以轻松访问.cs文件中的label文本值,因此javascript变量的主要问题是,您可以将此值分配给asp:hidden字段,这样您也可以在.cs文件中访问此值,如:
<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文件:
string str = hdn.value//Where ever you want to store value
2.)另一个选项可以在这里使用pagemethod,并将两个值作为参数传递给.cs文件。
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文件:
[WebMethod(enableSession: true)]
public static returntype getValue(parameter type parameter1,parameter type parameter2)
{
do what you want to do with these values.
}
希望对你有帮助。