客户端回调实现 (C#) 示例

来源:互联网 发布:talktothemoon知乎 编辑:程序博客网 时间:2024/04/27 21:34
  1. <%@ Page Language="C#" AutoEventWireup="true" 
  2.   CodeFile="ClientCallback.aspx.cs" Inherits="ClientCallback" %>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 
  4.   1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  5. <html  >
  6. <head id="Head1" runat="server">
  7.   <title>Client Callback Example</title>
  8.   <script type="text/ecmascript">
  9.     function LookUpStock()
  10.     {
  11.         var lb = document.getElementById("ListBox1");
  12.         var product = lb.options[lb.selectedIndex].text;
  13.         CallServer(product, "");
  14.     }
  15.     function ReceiveServerData(rValue)
  16.     {   
  17.         document.getElementById("ResultsSpan").innerHTML = rValue;
  18.     }
  19.   </script>
  20. </head>
  21. <body>
  22.   <form id="form1" runat="server">
  23.     <div>
  24.       <asp:ListBox ID="ListBox1" Runat="server"></asp:ListBox>
  25.       <br />
  26.       <br />
  27.       <button type="Button" onclick="LookUpStock()">Look Up Stock</button>
  28.       <br />
  29.       <br />
  30.       Items in stock: <span id="ResultsSpan" runat="server"></span>
  31.       <br />
  32.     </div>
  33.   </form>
  34. </body>
  35. </html>
    1. using System;
    2. using System.Data;
    3. using System.Configuration;
    4. using System.Collections;
    5. using System.Web;
    6. using System.Web.Security;
    7. using System.Web.UI;
    8. using System.Web.UI.WebControls;
    9. using System.Web.UI.WebControls.WebParts;
    10. using System.Web.UI.HtmlControls;
    11. public partial class ClientCallback : System.Web.UI.Page,
    12.      System.Web.UI.ICallbackEventHandler
    13. {
    14.     protected System.Collections.Specialized.ListDictionary catalog;
    15.     protected String returnValue;
    16.     protected void Page_Load(object sender, EventArgs e)
    17.     {
    18.         String cbReference =
    19.             Page.ClientScript.GetCallbackEventReference(this,
    20.             "arg""ReceiveServerData""context");
    21.         String callbackScript;
    22.         callbackScript = "function CallServer(arg, context)" +
    23.             "{ " + cbReference + ";}";
    24.         Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
    25.             "CallServer", callbackScript, true);
    26.         catalog = new System.Collections.Specialized.ListDictionary();
    27.         catalog.Add("monitor", 12);
    28.         catalog.Add("laptop", 10);
    29.         catalog.Add("keyboard", 23);
    30.         catalog.Add("mouse", 17);
    31.         ListBox1.DataSource = catalog;
    32.         ListBox1.DataTextField = "key";
    33.         ListBox1.DataBind();
    34.     }
    35.     public void RaiseCallbackEvent(String eventArgument)
    36.     {
    37.         if (catalog[eventArgument] == null)
    38.         {
    39.             returnValue = "-1";
    40.         }
    41.         else
    42.         {
    43.             returnValue = catalog[eventArgument].ToString();
    44.         }
    45.     }
    46.     public String GetCallbackResult()
    47.     {
    48.         return returnValue;
    49.     }
    50. }
原创粉丝点击