CallbackTest1.aspx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <%@ Page Language="C#" AutoEventWireup="true" %>
  2. <%@ Implements Interface="System.Web.UI.ICallbackEventHandler" %>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <script runat="server" >
  5. protected System.Collections.Specialized.ListDictionary catalog;
  6. protected String returnValue;
  7. public bool raiseCallbackEvent;
  8. public bool getCallbackResult;
  9. protected void Page_Load (object sender, EventArgs e)
  10. {
  11. String cbReference =
  12. Page.ClientScript.GetCallbackEventReference (this,
  13. "arg", "ReceiveServerData", "context", null, false);
  14. String callbackScript;
  15. callbackScript = "function CallServer(arg, context)" +
  16. "{ " + cbReference + ";}";
  17. Page.ClientScript.RegisterClientScriptBlock (this.GetType (),
  18. "CallServer", callbackScript, true);
  19. catalog = new System.Collections.Specialized.ListDictionary ();
  20. catalog.Add ("monitor", 12);
  21. catalog.Add ("laptop", 10);
  22. catalog.Add ("keyboard", 23);
  23. catalog.Add ("mouse", 17);
  24. ListBox1.DataSource = catalog;
  25. ListBox1.DataTextField = "key";
  26. ListBox1.DataBind ();
  27. }
  28. protected override void OnPreInit (EventArgs e)
  29. {
  30. MonoTests.SystemWeb.Framework.WebTest t = MonoTests.SystemWeb.Framework.WebTest.CurrentTest;
  31. if (t != null)
  32. t.Invoke (this);
  33. base.OnPreInit (e);
  34. }
  35. void System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent (String eventArgument)
  36. {
  37. raiseCallbackEvent = true;
  38. if (catalog[eventArgument] == null) {
  39. returnValue = "-1";
  40. }
  41. else {
  42. returnValue = catalog[eventArgument].ToString ();
  43. }
  44. }
  45. String System.Web.UI.ICallbackEventHandler.GetCallbackResult ()
  46. {
  47. getCallbackResult = true;
  48. if (getCallbackResult == true)
  49. returnValue += "|true";
  50. else
  51. returnValue += "|false";
  52. if (raiseCallbackEvent == true)
  53. returnValue += "|true";
  54. else
  55. returnValue += "|false";
  56. return returnValue;
  57. }
  58. </script>
  59. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
  60. <head id="Head1" runat="server">
  61. <title>Client Callback Example</title>
  62. <script type="text/ecmascript">
  63. function LookUpStock()
  64. {
  65. var lb = document.getElementById("ListBox1");
  66. var product = lb.options[lb.selectedIndex].text;
  67. CallServer(product, "");
  68. }
  69. function ReceiveServerData(rValue)
  70. {
  71. document.getElementById("ResultsSpan").innerHTML = rValue;
  72. }
  73. </script>
  74. </head>
  75. <body>
  76. <form id="form1" runat="server">
  77. <div>
  78. <asp:ListBox ID="ListBox1" Runat="server"></asp:ListBox>
  79. <br />
  80. <br />
  81. <button type="Button" onclick="LookUpStock()">Look Up Stock</button>
  82. <br />
  83. <br />
  84. Items in stock: <span id="ResultsSpan" runat="server"></span>
  85. <br />
  86. </div>
  87. </form>
  88. </body>
  89. </html>