JS調用asp.net後台方法:PageMethods

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   使用   io   for   

先幫朋友宣傳一下程式人生(http://www.manong123.com)的網站,裡面都是開發感悟,開發人員創業,支援一下吧~

原來是通過PageMethods來實現的。 

舉個列子: 

Default.aspx 裡代碼 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>無標題頁</title> <script type="text/javascript" language="javascript"> <!-- function minbzdm() { PageMethods.OK(xxx); } function xxx(result) { alert(result); } //--> </script> </head> <body>     <form id="form1" runat="server">     <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">     </asp:ScriptManager>     <div>     <input type=‘button‘ value=‘刪除‘ onclick=‘minbzdm()‘ />     </div>     </form> </body> </html> 

Default.aspx.cs裡的代碼 

public partial class _Default : System.Web.UI.Page {     protected void Page_Load(object sender, EventArgs e)      {      }      [System.Web.Services.WebMethod]     public static string OK()      {         return "OK";      } } 
public partial class _Default : System.Web.UI.Page {     protected void Page_Load(object sender, EventArgs e)      {      }      [System.Web.Services.WebMethod]     public static string OK()      {         return "OK";      } } 

通過PageMethods方法來實現JS調用CS,必須注意一下幾點: 

【1】靜態方法 

          public static 


【2】需要在cs方法上加上: 

         [System.Web.Services.WebMethod] 


【3】需要自訂一個函數接受結果 

        function xxx(result) 
        { 
        alert(result); 
        } 


【4】ScriptManager 必須設定成 EnablePageMethods="true" 

注意: 

1、PageMethods.OK( 參數一,參數二,參數三,...,xxx);可以傳入參數。對應的CS後台方法也要改進! 

2、xxx為回呼函數名,只有僅有一個參數。參數可以是數組,List<T>泛型等。 

http://www.cnblogs.com/juxiaoqi/archive/2008/08/21/1273059.html 
補充: 

AJAX 調用Web Service 與 PageMethods注意細節 

想要使用ASP.NET AJAX在用戶端JavaScript中非同步呼叫伺服器端Web Service,我們需要: 

1 為Web Service類或需要暴露給用戶端的Web Service方法添加[ScriptService] 
      屬性; 

2 為Web Service中需要暴露給用戶端的方法添加[WebMethod]屬性; 

3 在頁面中的ScriptManager控制項中添加對該Web Service的引用; 

4 在用戶端使用如下JavaScript文法調用該Web Service: 
      [NameSpace].[ClassName].[MethodName](param1, param2,..., callbackFunction) 

5 為用戶端非同步呼叫指定回呼函數,在回呼函數中接收傳回值並進一步處理。 


----------------------- 

想要使用ASP.NET AJAX在用戶端JavaScript中非同步呼叫定義在ASP.NET頁面中的方法,我們需要: 

1 將該方法聲明為公有(public); 

2 將該方法聲明為類方法(C#中的static,VB.NET中的Shared),而不是執行個體方法; 

3 為該方法添加[WebMethod]屬性; 

4 將頁面中ScriptManager控制項的EnablePageMethods屬性設定為true; 

5 在用戶端使用如下JavaScript文法調用該頁面方法: 
      PageMethods.[MethodName](param1, param2,..., callbackFunction); 

6 為用戶端非同步呼叫指定回呼函數,在回呼函數中接收傳回值並進一步處理。 

補充:另外一種調用 

前台JS: 

function check() {             var title = document.getElementById("TabContainer1_tabpanel4_txtPicTitle").value;             var sn = document.getElementById("TabContainer1_tabpanel4_txtSN").value;             var playGUID = document.getElementById("TabContainer1_tabpanel4_hidPlayGUID").value;             var msg = Drilling_Pictures.Validate(title, sn, playGUID).value;//主要注意這裡             if (msg == "") {                 return true;             } else {                 alert(msg);                 return false;             }         } <asp:ImageButton ID="imgBtn" runat="server" ImageUrl="~/images/btn25.png" OnClick="imgBtn_Click"                                                               OnClientClick="return check();" /> 

後台代碼: 

public partial class Drilling_Pictures : BasePage {     [AjaxPro.AjaxMethod]     public string Validate(string title, string sn, string playGUID)     {         string str = "";         if (title.Trim() == "")         {             str = "標題不可為空";         }         else if (sn.Trim() == "")         {             str = "序號不可為空";         }         else if (!ConvertInt(sn.Trim()))         {             str = "序號必須為整數";         }         else if (!new DrillingPlayBack().CheckSN(playGUID, int.Parse(sn)))         {             str = "序號已經存在";         }         return str;     } } 
  • 編寫優秀代碼的10條感悟 很好的一篇文章。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.