ClientDeserializeServerSerialize.aspx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <%@ Page Language="C#" AutoEventWireup="true"%>
  2. <%@ Import Namespace="System.Web.Script.Serialization" %>
  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. <head id="Head1" runat="server">
  6. <title>Client Deserialization Server Serialization</title>
  7. <style type="text/css" >
  8. body
  9. {
  10. font-family: Verdana, Arial, Helvetica, sans-serif;
  11. font-size: 80%;
  12. width: 100%;
  13. }
  14. </style>
  15. <!-- Client script performing deserialization -->
  16. <script type="text/javascript">
  17. // Color object constructor.
  18. // It functions also as the class definition for the object.
  19. function color() {
  20. // Define string property.
  21. this.message="client default color is Black";
  22. // Define an array property.
  23. this.rgb=['00', '00', '00'];
  24. // Define the a class member.
  25. }
  26. function OnClick_ClientDeserialize()
  27. {
  28. // Create the color object to use
  29. // for client deserialization.
  30. var jsonObject = new color();
  31. // Get the JSON string (serialized object)
  32. // stored by the server.
  33. //var jsonString =
  34. // document.getElementById('<%= ServerResultID.ClientID %>').innerText;
  35. var jsonString;
  36. if (document.all)
  37. jsonString =
  38. document.getElementById('<%= ServerResultID.ClientID %>').innerText;
  39. else
  40. jsonString =
  41. document.getElementById('<%= ServerResultID.ClientID %>').textContent;
  42. if (jsonString == "")
  43. {
  44. alert("blank");
  45. document.getElementById("ClientResultID").innerText =
  46. "Please, perform client serialization first.";
  47. document.getElementById("ClientResultID").style.backgroundColor =
  48. "red";
  49. }
  50. else
  51. {
  52. // Deserialize the JSON string
  53. // into the related client object.
  54. eval("jsonObject = " + jsonString);
  55. var message = jsonObject.message;
  56. var rgb = jsonObject.rgb;
  57. var serverColor = "";
  58. serverColor =
  59. serverColor.concat(rgb[0], rgb[1], rgb[2]);
  60. if (document.all)
  61. document.getElementById("ClientResultID").innerText = message + rgb;
  62. else
  63. document.getElementById("ClientResultID").textContent = message + rgb;
  64. document.getElementById("ClientResultID").style.backgroundColor = "#" + serverColor;
  65. }
  66. }
  67. </script>
  68. <!-- Sever script performing serialization -->
  69. <script runat="server">
  70. // Define the object to serialize.
  71. class ColorObject
  72. {
  73. public string message = "Server default color is Black ";
  74. public string[] rgb = new string[] { "00", "00", "00" };
  75. }
  76. public void OnClick_ServerSerialize(object sender, EventArgs e)
  77. {
  78. // Instantiate a serializer.
  79. System.Web.Script.Serialization.JavaScriptSerializer serializer =
  80. new JavaScriptSerializer();
  81. // Perform serialization.
  82. ColorObject co = new ColorObject();
  83. string serializedServerObject = serializer.Serialize(co);
  84. ServerResultID.Text = serializedServerObject;
  85. }
  86. </script>
  87. </head>
  88. <body>
  89. <h2>Client Deserialization Server Serialization</h2>
  90. <!-- Add the script manager -->
  91. <form id="form1" runat="server">
  92. <asp:ScriptManager runat="server" ID="ScriptManager1"/>
  93. <h3>Server</h3>
  94. <!-- Perform server serialization -->
  95. <asp:Button ID="ServerButtonID" OnClick="OnClick_ServerSerialize"
  96. Text="Server Serialize" runat="server" />
  97. <p>
  98. <span style="background-color:Yellow">Server serialization:</span>
  99. <asp:Label ID="ServerResultID" runat="server" />
  100. </p>
  101. <hr />
  102. <h2>Client</h2>
  103. <!-- Perform client deserialization -->
  104. <button id="ClientButtonID"
  105. onclick="OnClick_ClientDeserialize(); return false;">Client Deserialize</button>
  106. <!-- Store client deserialization and preserve it on postback so
  107. the server can read it and perform its deserialization -->
  108. <p>
  109. <span style="background-color:Yellow">Client deserialization:</span>
  110. <span id="ClientResultID"
  111. style="background-color:Aqua; font-weight:bold; color:Yellow" ></span>
  112. </p>
  113. <hr />
  114. </form>
  115. </body>
  116. </html>