關於.net的異步刷新機制 ICallbackEventHandler

      最近在做一個項目,有一個頁面用到異步刷新,因爲不想用updatepanel處理,就想到了.net自帶的ICallbackEventHandler接口,在這裏簡單的對照msdn寫了一個列子。

      既然要異步,那麼肯定要手動在前臺寫部分js了,既然是交互,那麼傳值這一步是少不了的了,顯然首先要有一個調取的頁面元素值得方法   

 

 

  1. function LookUpStock() { 
  2.  
  3.     var lb = document.getElementById("ListBox1"); 
  4.  
  5.     var product = lb.options[lb.selectedIndex].text; 
  6.  
  7.     CallServer(product, ""); 
  8.  

那麼既然是異步更新數據,那麼修改後的值肯定是js進行操作的了,這裏就添加了一個返回值的js方法

 

 

  1. function ReceiveServerData(rValue) { 
  2.  
  3.     document.getElementById("ResultsSpan").innerHTML = rValue; 
  4.  

前臺頁面的html內容如下:

 

 

  1. <html xmlns="http://www.w3.org/1999/xhtml" > 
  2.  
  3. <head id="Head1" runat="server"> 
  4.  
  5.   <title>Client Callback Example</title> 
  6.  
  7.   <script type="text/ecmascript"> 
  8.  
  9.       function LookUpStock() { 
  10.  
  11.           var lb = document.getElementById("ListBox1"); 
  12.  
  13.           var product = lb.options[lb.selectedIndex].text; 
  14.  
  15.           CallServer(product, ""); 
  16.  
  17.       } 
  18.  
  19.  
  20.  
  21.       function ReceiveServerData(rValue) { 
  22.  
  23.           document.getElementById("ResultsSpan").innerHTML = rValue
  24.  
  25.       } 
  26.  
  27.   </script> 
  28.  
  29. </head> 
  30.  
  31. <body> 
  32.  
  33.   <form id="form1" runat="server"> 
  34.  
  35.     <div> 
  36.  
  37.       <asp:ListBox ID="ListBox1" Runat="server"></asp:ListBox> 
  38.  
  39.       <br /> 
  40.  
  41.       <br /> 
  42.  
  43.       <button type="Button" onclick="LookUpStock()">Look Up Stock</button> 
  44.  
  45.       <br /> 
  46.  
  47.       <br /> 
  48.  
  49.       Items in stock: <span id="ResultsSpan" runat="server"></span> 
  50.  
  51.       <br /> 
  52.  
  53.     </div> 
  54.  
  55.   </form> 
  56.  
  57. </body> 
  58.  
  59. </html> 

在後臺:

 

  1. public partial class Default2 : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler 
  2.     protected System.Collections.Specialized.ListDictionary catalog; 
  3.     protected String returnValue;//用來得到返回值 
  4.     protected void Page_Load(object sender, EventArgs e) 
  5.     { 
  6.         String cbReference = 
  7.             Page.ClientScript.GetCallbackEventReference(this
  8.             "arg""ReceiveServerData""context");//添加頁面回傳觸發操作 
  9.         String callbackScript; 
  10.         callbackScript = "function CallServer(arg, context)" + 
  11.             "{ " + cbReference + ";}"
  12.        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), 
  13.             "CallServer", callbackScript, true); 
  14. //註冊前臺CallServer方法,方法內容就是  callbackScript  
  15.         catalog = new System.Collections.Specialized.ListDictionary(); 
  16.         catalog.Add("monitor", 12); 
  17.         catalog.Add("laptop", 10); 
  18.         catalog.Add("keyboard", 23); 
  19.         catalog.Add("mouse", 17); 
  20.  
  21.         ListBox1.DataSource = catalog; 
  22.         ListBox1.DataTextField = "key"
  23.         ListBox1.DataBind(); 
  24.  
  25.     } 
  26. //這裏是繼承自ICallbackEventHandler的方法,將頁面的值傳入處理 
  27.     public void RaiseCallbackEvent(String eventArgument) 
  28.     { 
  29.         if (catalog[eventArgument] == null
  30.         { 
  31.             returnValue = "-1"
  32.         } 
  33.         else 
  34.         { 
  35.             returnValue = catalog[eventArgument].ToString(); 
  36.         } 
  37.     } 
  38. //這裏是繼承自ICallbackEventHandler的方法 
  39. //將處理後的值返回,供頁面使用 
  40.     public String GetCallbackResult() 
  41.     { 
  42.         return returnValue; 
  43.     } 
  44. }


 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章