從客戶端調用后臺靜態方法
1.Ajax Library方式
C#代碼:
[WebMethod]
public static DateTime GetCurrentTime(string str)
{
return DateTime.Now;
}
JS代碼:
<form id="form1" runat="server">
<script language=javascript type="text/javascript">
function GetCurrentTime1() {
PageMethods.GetCurrentTime('NewEgg ajax training', CheckIsSuccess);
}
function CheckIsSuccess(result) {
alert(result);
}
</script>
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<input id="Button1" type="button" value="客戶端控件調用服務器端的方法" onclick="GetCurrentTime1()" />
</div>
</form>
說明:
C#方法必須加 "[WebMethod]"
前臺頁面必須使用引用 服務器控件
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
調用方法: PageMethods.后臺方法名(參數[,參數....], 成功后調用的方法名);
Ajax Library
2. jQuery方式 C#代碼:
[WebMethod]
public static string ABC(string ABC)
{
return ABC;
}
JS代碼:
$().ready(
function() {
$("#AjaxDemo").click(function() {
$.ajax({
type: "POST",
url: "Default.aspx/ABC",
data: "{'ABC':'test'}",
contentType: "application/json; charset=utf-8",
success: function(msg) {alert(msg); }
})
})
}
)
說明: 必須引用jQuery庫文件.
3. 還有一種好像是要引用AJAX.dll文件的. 在后臺注冊前臺方法. 這個好像在.net2.0的時候用的比較多. 具體沒仔細研究.
還望有其他更簡單方法的同學互相交流下~
新聞熱點
疑難解答
圖片精選