程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#實現客戶端回調數據代碼

C#實現客戶端回調數據代碼

編輯: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. }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved