当前位置:编程学习 > C#/ASP.NET >>

C#实现客户端回调数据代码

  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>
  36. using System;
  37. using System.Data;
  38. using System.Configuration;
  39. using System.Collections;
  40. using System.Web;
  41. using System.Web.Security;
  42. using System.Web.UI;
  43. using System.Web.UI.WebControls;
  44. using System.Web.UI.WebControls.WebParts;
  45. using System.Web.UI.HtmlControls;
  46. public partial class ClientCallback : System.Web.UI.Page,
  47.      System.Web.UI.ICallbackEventHandler
  48. {
  49.     protected System.Collections.Specialized.ListDictionary catalog;
  50.     protected String returnValue;
  51.     protected void Page_Load(object sender, EventArgs e)
  52.     {
  53.         String cbReference =
  54.             Page.ClientScript.GetCallbackEventReference(this,
  55.             "arg", "ReceiveServerData", "context");
  56.         String callbackScript;
  57.         callbackScript = "function CallServer(arg, context)"
  58.             "{ " cbReference ";}";
  59.         Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
  60.             "CallServer", callbackScript, true);
  61.         catalog = new System.Collections.Specialized.ListDictionary();
  62.         catalog.Add("monitor", 12);
  63.         catalog.Add("laptop", 10);
  64.         catalog.Add("keyboard", 23);
  65.         catalog.Add("mouse", 17);
  66.         ListBox1.DataSource = catalog;
  67.         ListBox1.DataTextField = "key";
  68.         ListBox1.DataBind();
  69.     }
  70.     public void RaiseCallbackEvent(String eventArgument)
  71.     {
  72.         if (catalog[eventArgument] == null)
  73.         {
  74.             returnValue = "-1";
  75.         }
  76.         else
  77.         {
  78.             returnValue = catalog[eventArgument].ToString();
  79.         }
  80.     }
  81.     public String GetCallbackResult()
  82.     {
  83.         return returnValue;
  84.     }
  85. }

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,