Welcome Guest! You need to login or register to make posts.

Notification

Icon
Error

Options
Go to last post Go to first unread
Fedor  
#1 Posted : Wednesday, January 1, 2003 6:00:00 AM(UTC)
Fedor

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 7/28/2003(UTC)
Posts: 1,660

Thanks: 5 times
Was thanked: 76 time(s) in 74 post(s)
ASP.NET allows to serialize Web Controls and save their state in the ViewState property of the page. This process is called serialization. The inverse process (restoring previously serialized state) is called deserialization. .NET Framework provides serialization/deserialization for View State automatically. However sometimes we need to save/restore Web Control state manually somewhere else, for example to Session variable. So we need to follow all the steps which .NET Framework does. This article demonstrates how to do it.

The state of Web Controls is stored on page in hidden field in encrypted way between user visits to page.

Code:
<input type="hidden" name="__VIEWSTATE" value="dDwtMTIxMzQ" />


To serialize/deserialize control state, ASP.NET calls standard pair of methods SaveViewState / LoadViewState. The first one saves state to ViewState property, the second - restores it. ASP.NET calls these methods passing ViewState to it. So our task is to create "bogus" ViewState, call these methods and save received data into Session variable (or somewhere else). The problem is that methods LoadViewState and SaveViewState are protected (not public), so we cannot directly call it. However .NET Framework provides great tool called Reflection. In particular it allows us to call protected methods. These code samples demonstrate how to serialize/deserialize FlashChart to/from Session variable. Nevertheless you can use this approach for any other controls which supports View State technology and save not only to Session, but somewhere else, for example database.
Code:

[C#]
<%@ Page language="C#"%>
<%@ Register tagprefix="FC" Namespace="Aurigma.Charting" Assembly="Aurigma.Charting" %>

<%@ Import Namespace="System.Reflection"%>
<%
// Create an instance of the FlashChart
FlashChart objFlashChart = new FlashChart();

//  Fill it with data
objFlashChart.Data.SetDimension(3,1);
objFlashChart.Data.DataCategories[0].Caption = "Income";

objFlashChart.Data.DataSeries[0].Caption = "June";
objFlashChart.Data.DataSeries[0][0] = 10;

objFlashChart.Data.DataSeries[1].Caption = "July";
objFlashChart.Data.DataSeries[1][0] = 20;

objFlashChart.Data.DataSeries[2].Caption = "August";
objFlashChart.Data.DataSeries[2][0] = 40;

// Save it to file
objFlashChart.Save(Server.MapPath("Serialized.swf"));

// Start serialization
// First we should get Type object, which represent a type of Flash Chart class
Type objChartType  = objFlashChart.GetType();

// After it just call necessary method via InvokeMember method. Read about it more

// detailed in .NET Framework SDK. 
// Draw attention on the first and next to last parameters. 
// The first one is a name of the method, the next to last is an instance of FlashChart. 
// Array objChartData will hold serialized FlashChart
Object objChartData = objChartType.InvokeMember("SaveViewState", 
	BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance, 
	null, 
	objFlashChart, // Here we should pass our FlashChart object

	null);

// Save serialized data to Session variable
Session["FlashChart"] = objChartData;

%>
<p>Flash Chart is serialized!</p>


Deserialize it in the similar way:

Code:
[C#]
<%@ Page language="C#"%>

<%@ Register tagprefix="FC" Namespace="Aurigma.Charting" Assembly="Aurigma.Charting" %>
<%@ Import Namespace="System.Reflection"%>

<%
// Create an instance of the FlashChart
FlashChart objFlashChart = new FlashChart();

// Deserialize it from session variable

// objChartData	is an array which contains serialized data.
// It should be taken from Session variable (or somewhere else where you store it).

// Draw attention how we create the array. It is an array with one entry which. 
// contains an array we retrieve from Session. Why we do it so tricky? The last parameter of the 
// InvokeMember method is an array which holds parameters of calling method. So it should contain 
// as much entries as amount of parameter of given method. In our case it is the single parameter - 
// array of serialized data. In the first sample we passed null into last parameter, because method
// SaveViewState does not require any parameters.

Object[] objChartData = new Object[1];

objChartData[0] = Session["FlashChart"]; 

Type objChartType  = objFlashChart.GetType();

// Deserialize our chart from objChartData 
objChartType.InvokeMember("LoadViewState", 
	BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance, 
	null, 
	objFlashChart, // Here we should pass our FlashChart object

	objChartData); // Here we pass data for deserialization

// Save it to file
objFlashChart.Save(Server.MapPath("Deserialized.swf"));

%>
<p>Flash Chart is deserialized!</p>

Best regards,
Fedor Skvortsov
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.