EventValidationTest2.aspx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <%@ Page Language="C#" %>
  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. <html xmlns="http://www.w3.org/1999/xhtml" >
  5. <script runat="server" >
  6. string _eventName = "Fail";
  7. string _cbMessage = "";
  8. // Define method that processes the callbacks on server.
  9. public void RaiseCallbackEvent(String eventArgument)
  10. {
  11. try {
  12. this.ClientScript.ValidateEvent (_eventName, this.ToString ());
  13. _cbMessage = "Correct event raised callback.";
  14. }
  15. catch (Exception ex) {
  16. _cbMessage = "Incorrect event raised callback.";
  17. }
  18. }
  19. // Define method that returns callback result.
  20. public string GetCallbackResult()
  21. {
  22. return _cbMessage;
  23. }
  24. protected void Page_Load(object sender, EventArgs e)
  25. {
  26. if (!IsPostBack)
  27. {
  28. ClientScriptManager cs = Page.ClientScript;
  29. String cbReference = cs.GetCallbackEventReference ("'" +
  30. Page.UniqueID + "'", "arg", "ReceiveServerData", "",
  31. "ProcessCallBackError", false);
  32. String callbackScript = "function CallTheServer(arg, context) {" +
  33. cbReference + "; }";
  34. cs.RegisterClientScriptBlock (this.GetType (), "CallTheServer",
  35. callbackScript, true);
  36. }
  37. }
  38. protected override void Render(HtmlTextWriter writer)
  39. {
  40. this.ClientScript.RegisterForEventValidation("Test", this.ToString());
  41. base.Render(writer);
  42. }
  43. </script>
  44. <script type="text/javascript">
  45. var value1 = new Date();
  46. function ReceiveServerData(arg, context)
  47. {
  48. Message.innerText = arg;
  49. Label1.innerText = "Callback completed at " + value1;
  50. value1 = new Date();
  51. }
  52. function ProcessCallBackError(arg, context)
  53. {
  54. Message.innerText = 'An error has occurred.';
  55. }
  56. </script>
  57. <html >
  58. <head id="Head1" runat="server">
  59. <title>CallBack Event Validation Example</title>
  60. </head>
  61. <body>
  62. <form id="Form1" runat="server">
  63. <div>
  64. Callback result: <span id="Message"></span>
  65. <br /> <br />
  66. <input type="button"
  67. id="button1"
  68. runat="server"
  69. value="ClientCallBack"
  70. onclick="CallTheServer(value1, null )"/>
  71. <br /> <br />
  72. <asp:Label id="Label1" runat="server"/>
  73. </div>
  74. </form>
  75. </body>
  76. </html>