| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <%@ 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 Deserialization Server Serialization</title>
- <style type="text/css" >
- body
- {
- font-family: Verdana, Arial, Helvetica, sans-serif;
- font-size: 80%;
- width: 100%;
- }
- </style>
-
- <!-- Client script performing deserialization -->
- <script type="text/javascript">
- // Color object constructor.
- // It functions also as the class definition 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.
- }
-
- function OnClick_ClientDeserialize()
- {
-
- // Create the color object to use
- // for client deserialization.
- var jsonObject = new color();
-
- // Get the JSON string (serialized object)
- // stored by the server.
- //var jsonString =
- // document.getElementById('<%= ServerResultID.ClientID %>').innerText;
-
- var jsonString;
-
- if (document.all)
- jsonString =
- document.getElementById('<%= ServerResultID.ClientID %>').innerText;
- else
- jsonString =
- document.getElementById('<%= ServerResultID.ClientID %>').textContent;
-
- if (jsonString == "")
- {
- alert("blank");
- document.getElementById("ClientResultID").innerText =
- "Please, perform client serialization first.";
- document.getElementById("ClientResultID").style.backgroundColor =
- "red";
- }
- else
- {
- // Deserialize the JSON string
- // into the related client object.
- eval("jsonObject = " + jsonString);
-
- var message = jsonObject.message;
- var rgb = jsonObject.rgb;
-
- var serverColor = "";
- serverColor =
- serverColor.concat(rgb[0], rgb[1], rgb[2]);
-
- if (document.all)
- document.getElementById("ClientResultID").innerText = message + rgb;
- else
- document.getElementById("ClientResultID").textContent = message + rgb;
-
- document.getElementById("ClientResultID").style.backgroundColor = "#" + serverColor;
- }
-
- }
-
- </script>
-
-
-
- <!-- Sever script performing serialization -->
- <script runat="server">
- // Define the object to serialize.
- class ColorObject
- {
- public string message = "Server default color is Black ";
- public string[] rgb = new string[] { "00", "00", "00" };
- }
-
- public void OnClick_ServerSerialize(object sender, EventArgs e)
- {
- // Instantiate a serializer.
- System.Web.Script.Serialization.JavaScriptSerializer serializer =
- new JavaScriptSerializer();
-
- // Perform serialization.
- ColorObject co = new ColorObject();
-
- string serializedServerObject = serializer.Serialize(co);
- ServerResultID.Text = serializedServerObject;
-
- }
- </script>
- </head>
-
- <body>
-
- <h2>Client Deserialization Server Serialization</h2>
-
- <!-- Add the script manager -->
- <form id="form1" runat="server">
- <asp:ScriptManager runat="server" ID="ScriptManager1"/>
-
-
- <h3>Server</h3>
- <!-- Perform server serialization -->
- <asp:Button ID="ServerButtonID" OnClick="OnClick_ServerSerialize"
- Text="Server Serialize" runat="server" />
-
- <p>
- <span style="background-color:Yellow">Server serialization:</span>
- <asp:Label ID="ServerResultID" runat="server" />
- </p>
-
- <hr />
-
- <h2>Client</h2>
- <!-- Perform client deserialization -->
- <button id="ClientButtonID"
- onclick="OnClick_ClientDeserialize(); return false;">Client Deserialize</button>
-
- <!-- Store client deserialization and preserve it on postback so
- the server can read it and perform its deserialization -->
- <p>
- <span style="background-color:Yellow">Client deserialization:</span>
- <span id="ClientResultID"
- style="background-color:Aqua; font-weight:bold; color:Yellow" ></span>
- </p>
-
-
- <hr />
-
-
-
- </form>
-
- </body>
-
- </html>
|