最近在做一个项目,有一个页面用到异步刷新,因为不想用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. }