ClientSerializeServerDeserialize.aspx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 Serialization Server Deserialization</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 serialization -->
  16. <script type="text/JavaScript">
  17. // Color object constructor.
  18. // It is also the class definittion 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. this.setColor = setColor;
  26. }
  27. // It changes the color value stored in the object.
  28. function setColor(message, rgbValue)
  29. {
  30. // Build an array from the comma
  31. // separated string parameter.
  32. var rgbValueArray = rgbValue.split(",");
  33. // Assign the message.
  34. this.message=message;
  35. // Assign the rgb value.
  36. this.rgb=rgbValueArray;
  37. }
  38. function OnClick_ClientSerialize()
  39. {
  40. // Create the color object to use
  41. // for client serialization.
  42. var jsonObject = new color();
  43. // Serialize the object to obtain
  44. // the related JSON string.
  45. var jsonString =
  46. Sys.Serialization.JavaScriptSerializer.serialize(jsonObject);
  47. if (document.all)
  48. {
  49. // Display the client's serialization.
  50. document.getElementById('<%= ClientResultID.ClientID %>').innerText =
  51. jsonString;
  52. // Store the serialized object for the server.
  53. document.getElementById('<%= InputID.ClientID %>').innerText =
  54. jsonString;
  55. }
  56. else
  57. { // Firefox
  58. document.getElementById('<%= ClientResultID.ClientID %>').textContent =
  59. jsonString;
  60. // Store the serialized object for the server.
  61. document.getElementById('<%= InputID.ClientID %>').value =
  62. jsonString;
  63. }
  64. }
  65. // It changes the color
  66. // and stores it for the server.
  67. function OnColorSelected()
  68. {
  69. var selectionValue =
  70. document.getElementById("ColorSelectID").value;
  71. var selectionIndex =
  72. document.getElementById("ColorSelectID").selectedIndex;
  73. var selectedColor =
  74. document.getElementById("ColorSelectID").options[selectionIndex].text;
  75. // Create a color object.
  76. var myColor = new color();
  77. // Assign the new selected value.
  78. var message = "client color is " + selectedColor;
  79. var rgb = selectionValue;
  80. // Assign the selected value to the color object.
  81. myColor.setColor(message, rgb);
  82. // Serialize the color object to obtain
  83. // the related JSON string.
  84. var jsonString =
  85. Sys.Serialization.JavaScriptSerializer.serialize(myColor);
  86. if (document.all)
  87. {
  88. // Display the client's serialization.
  89. document.getElementById('<%= ClientResultID.ClientID %>').innerText =
  90. jsonString;
  91. // Store the serialized object for the server.
  92. document.getElementById('<%= InputID.ClientID %>').innerText =
  93. jsonString;
  94. }
  95. else
  96. { // Firefox
  97. document.getElementById('<%= ClientResultID.ClientID %>').textContent =
  98. jsonString;
  99. // Store the serialized object for the server.
  100. document.getElementById('<%= InputID.ClientID %>').value =
  101. jsonString;
  102. }
  103. }
  104. </script>
  105. <!-- Sever script performing deserialization -->
  106. <script runat="server">
  107. // Define the color object to serialize.
  108. class ColorObject
  109. {
  110. public string message = "server color";
  111. public string[] rgb =
  112. new string[] { "00", "00", "00" };
  113. public ColorObject()
  114. {
  115. this.message = "Server color is black.";
  116. this.rgb = new string[] { "00", "00", "00" };
  117. }
  118. }
  119. public void OnClick_ServerDeSerialize(object sender,
  120. EventArgs e)
  121. {
  122. // Instantiate a serializer.
  123. System.Web.Script.Serialization.JavaScriptSerializer serializer =
  124. new JavaScriptSerializer();
  125. // Get the client serialized object (JSON string).
  126. string clientSerializedObject = InputID.Value;
  127. ClientResultID.InnerText = InputID.Value;
  128. // Perform deserialization and
  129. // get back the color object.
  130. ColorObject co =
  131. serializer.Deserialize<ColorObject>(clientSerializedObject);
  132. if (co == null)
  133. {
  134. ServerResultID.InnerText =
  135. "Please, perform client serialization first.";
  136. ServerResultID.Style.Value =
  137. "color:red;" + " font-weight:bold;";
  138. }
  139. else
  140. {
  141. string rgb = co.rgb[0] + co.rgb[1] + co.rgb[2];
  142. ServerResultID.InnerText = co.message + ": " + rgb;
  143. ServerResultID.Style.Value =
  144. "color:yellow;" + " font-weight:bold;" +
  145. " background-color:#" + rgb;
  146. }
  147. }
  148. </script>
  149. </head>
  150. <body>
  151. <!-- Add the script manager -->
  152. <form id="form1" runat="server">
  153. <asp:ScriptManager runat="server" ID="ScriptManager1"/>
  154. <h2>Client Serialization Server Deserialization</h2>
  155. <h3>Client</h3>
  156. <!-- Perform client serialization -->
  157. <span class="yellow_marker">Serialize client default color:</span>
  158. <button id="ClientButtonID"
  159. onclick="OnClick_ClientSerialize(); return false;">Default Color</button>
  160. <!-- Store client serialization and preserve it
  161. on postback so the server can read it and
  162. perform its deserialization -->
  163. <input
  164. id="InputID"
  165. type="text"
  166. style="width:300px; visibility:hidden"
  167. runat="server"/>
  168. <p>
  169. <span class="yellow_marker">Serialize new client color:</span>
  170. <select id="ColorSelectID" OnChange="OnColorSelected(); return false; " runat="server" >
  171. <option value="00,00,00">Black</option>
  172. <option value="FF,00,00">Red</option>
  173. <option value="00,FF,00">Green</option>
  174. <option value="00,00,FF">Blue</option>
  175. </select>
  176. </p>
  177. <span
  178. style="background-color:Yellow">Client serialization:</span>
  179. <span id="ClientResultID" runat="server" enableviewstate="true"> </span>
  180. <hr />
  181. <h3>Server</h3>
  182. <!-- Perform server deserialization -->
  183. <asp:Button ID="ServerButtonID" OnClick="OnClick_ServerDeSerialize"
  184. Text="Server Deserialize" runat="server" />
  185. <p>
  186. <span style="background-color:Yellow">Server deserialization:</span>
  187. <span id="ServerResultID" style="background-color:Aqua;" runat="server"/>
  188. </p>
  189. </form>
  190. <hr />
  191. </body>
  192. </html>