[英]How to access a javascript object from .js file in C# code behind file .aspx.cs file
[英]Access javascript variable's value in .cs file that is in in code behind?
我想訪問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>
要將客戶端信息(JavaScript結果/變量)發送到服務器,您必須...將信息發送到服務器。 :-)您可以通過多種方式來執行此操作,更新數據庫的兩種最佳方法是:
創建服務器端隱藏字段,將javascript值放入其中。 並讀取服務器端的表單隱藏值字段。
HTML
<input type="hidden" id="myhidden" runat="server">
使用javascript訪問
document.getElementById("myhidden").value="jsvalue";
在后面的代碼中訪問
myhidden.Value
要么
您可以使用ajax將數據發送到服務器。
要使用此值,您可以使用以下選項:
1.)由於label是服務器端控件,因此您可以輕松訪問.cs文件中的標簽文本值,因此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.
}
希望對您有幫助。
這是javascript和aspx頁面
<script language="Javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script language="Javascript" type="text/javascript" src="http://api.easyjquery.com/easyjquery.js"></script>
<script language=Javascript>
function setup_ip(json) {
var htmlx = " Your IP Address: <b>" + json.IP;
htmlx += "</b> | Country: " + json.countryName;
var s = " | City: " + json.cityName + " / " + json.regionName;
$("#hdn").val(s);
if (json.cityName != "Unknown" || json.regionName != "Unknown") {
htmlx += " | City: " + json.cityName + " / " + json.regionName;
s = " | City: " + json.cityName + " / " + json.regionName;
} else {
htmlx += " | Your Time: " + json.localTimeZone;
}
}
$(document).ready(function() {
EasyjQuery_Get_IP("setup_ip", "full");
});
function testValue() {
$('#hdn').val();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<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>
<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" ></a>
<asp:TextBox ID="loc" runat="server"></asp:TextBox>
</p>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="setup_ip(json);" onclick="Button1_Click" />
</div>
</div>
</form>
</body>
</html>
這是.cs代碼
protected void Page_Load(object sender, EventArgs e)
{
loc.Text = hdn.Value;
}
protected void Button1_Click(object sender, EventArgs e)
{
string str = hdn.Value;
}
您可以使用ajax請求將數據發布到服務器。 這非常簡單,因為您已經在使用jquery。 請參閱api ref http://api.jquery.com/jQuery.ajax/ 。
$.ajax({
type: "POST",
url: "Page.aspx/method_name",
data: { name: "some data", id: "some more data" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
對於此示例,在Page.aspx的代碼隱藏中,您將有一個名為method_name的方法,該方法帶有兩個參數:字符串名稱,字符串id。 該方法將用[WebMethod]
屬性修飾。 然后,您可以按自己喜歡的方式使用這些方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.