文章目錄
- 1.Ajax Library方式
- 2. jQuery方式
- 3. 還有一種好像是要引用AJAX.dll檔案的. 在後台註冊前台方法. 這個好像在.net2.0的時候用的比較多. 具體沒仔細研究.
從用戶端調用後台靜態方法
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的時候用的比較多. 具體沒仔細研究.
還望有其他更簡單方法的同學互相交流下~