| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <%@ Page Language="C#" AutoEventWireup="true"%>
- <%@ Import Namespace="System.Web.Script.Serialization" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head id="Head1" runat="server">
-
- <title>Client Serialization Server Deserialization</title>
- <style type="text/css" >
- body
- {
- font-family: Verdana, Arial, Helvetica, sans-serif;
- font-size: 80%;
- width: 100%;
- }
- </style>
-
-
- <!-- Client script performing serialization -->
- <script type="text/JavaScript">
-
- // Color object constructor.
- // It is also the class definittion for the object.
- function color() {
- // Define string property.
- this.message="client default color is Black";
- // Define an array property.
- this.rgb=['00', '00', '00'];
-
- // Define the a class member.
- this.setColor = setColor;
- }
- // It changes the color value stored in the object.
- function setColor(message, rgbValue)
- {
- // Build an array from the comma
- // separated string parameter.
- var rgbValueArray = rgbValue.split(",");
- // Assign the message.
- this.message=message;
- // Assign the rgb value.
- this.rgb=rgbValueArray;
- }
- function OnClick_ClientSerialize()
- {
-
- // Create the color object to use
- // for client serialization.
- var jsonObject = new color();
-
- // Serialize the object to obtain
- // the related JSON string.
- var jsonString =
- Sys.Serialization.JavaScriptSerializer.serialize(jsonObject);
-
-
- if (document.all)
- {
- // Display the client's serialization.
- document.getElementById('<%= ClientResultID.ClientID %>').innerText =
- jsonString;
- // Store the serialized object for the server.
- document.getElementById('<%= InputID.ClientID %>').innerText =
- jsonString;
- }
- else
- { // Firefox
-
- document.getElementById('<%= ClientResultID.ClientID %>').textContent =
- jsonString;
- // Store the serialized object for the server.
- document.getElementById('<%= InputID.ClientID %>').value =
- jsonString;
- }
-
-
- }
-
- // It changes the color
- // and stores it for the server.
- function OnColorSelected()
- {
-
- var selectionValue =
- document.getElementById("ColorSelectID").value;
-
- var selectionIndex =
- document.getElementById("ColorSelectID").selectedIndex;
- var selectedColor =
- document.getElementById("ColorSelectID").options[selectionIndex].text;
-
- // Create a color object.
- var myColor = new color();
-
- // Assign the new selected value.
- var message = "client color is " + selectedColor;
- var rgb = selectionValue;
-
- // Assign the selected value to the color object.
- myColor.setColor(message, rgb);
-
- // Serialize the color object to obtain
- // the related JSON string.
- var jsonString =
- Sys.Serialization.JavaScriptSerializer.serialize(myColor);
-
- if (document.all)
- {
- // Display the client's serialization.
- document.getElementById('<%= ClientResultID.ClientID %>').innerText =
- jsonString;
- // Store the serialized object for the server.
- document.getElementById('<%= InputID.ClientID %>').innerText =
- jsonString;
- }
- else
- { // Firefox
-
- document.getElementById('<%= ClientResultID.ClientID %>').textContent =
- jsonString;
- // Store the serialized object for the server.
- document.getElementById('<%= InputID.ClientID %>').value =
- jsonString;
- }
-
-
- }
- </script>
-
- <!-- Sever script performing deserialization -->
- <script runat="server">
- // Define the color object to serialize.
- class ColorObject
- {
- public string message = "server color";
- public string[] rgb =
- new string[] { "00", "00", "00" };
- public ColorObject()
- {
- this.message = "Server color is black.";
- this.rgb = new string[] { "00", "00", "00" };
- }
- }
-
- public void OnClick_ServerDeSerialize(object sender,
- EventArgs e)
- {
- // Instantiate a serializer.
- System.Web.Script.Serialization.JavaScriptSerializer serializer =
- new JavaScriptSerializer();
-
-
- // Get the client serialized object (JSON string).
- string clientSerializedObject = InputID.Value;
- ClientResultID.InnerText = InputID.Value;
-
- // Perform deserialization and
- // get back the color object.
- ColorObject co =
- serializer.Deserialize<ColorObject>(clientSerializedObject);
- if (co == null)
- {
- ServerResultID.InnerText =
- "Please, perform client serialization first.";
- ServerResultID.Style.Value =
- "color:red;" + " font-weight:bold;";
- }
- else
- {
- string rgb = co.rgb[0] + co.rgb[1] + co.rgb[2];
- ServerResultID.InnerText = co.message + ": " + rgb;
- ServerResultID.Style.Value =
- "color:yellow;" + " font-weight:bold;" +
- " background-color:#" + rgb;
- }
-
- }
- </script>
-
- </head>
-
- <body>
-
-
- <!-- Add the script manager -->
- <form id="form1" runat="server">
- <asp:ScriptManager runat="server" ID="ScriptManager1"/>
-
- <h2>Client Serialization Server Deserialization</h2>
-
- <h3>Client</h3>
-
- <!-- Perform client serialization -->
- <span class="yellow_marker">Serialize client default color:</span>
- <button id="ClientButtonID"
- onclick="OnClick_ClientSerialize(); return false;">Default Color</button>
-
- <!-- Store client serialization and preserve it
- on postback so the server can read it and
- perform its deserialization -->
- <input
- id="InputID"
- type="text"
- style="width:300px; visibility:hidden"
- runat="server"/>
-
- <p>
- <span class="yellow_marker">Serialize new client color:</span>
- <select id="ColorSelectID" OnChange="OnColorSelected(); return false; " runat="server" >
- <option value="00,00,00">Black</option>
- <option value="FF,00,00">Red</option>
- <option value="00,FF,00">Green</option>
- <option value="00,00,FF">Blue</option>
- </select>
- </p>
-
- <span
- style="background-color:Yellow">Client serialization:</span>
- <span id="ClientResultID" runat="server" enableviewstate="true"> </span>
-
-
-
- <hr />
-
- <h3>Server</h3>
-
- <!-- Perform server deserialization -->
- <asp:Button ID="ServerButtonID" OnClick="OnClick_ServerDeSerialize"
- Text="Server Deserialize" runat="server" />
-
- <p>
- <span style="background-color:Yellow">Server deserialization:</span>
- <span id="ServerResultID" style="background-color:Aqua;" runat="server"/>
- </p>
-
-
- </form>
-
- <hr />
-
-
- </body>
-
- </html>
|