State
management is the process to maintain state and page information over multiple
requests for the same or different pages. Web applications are based on
stateless HTTP protocol so web form pages do not automatically indicate whether
the requests in a sequence are all from the same client or even whether a single
browser instance is still actively viewing a page or site. A new instance of the
Web page class is created each time the page is requested and with each round
trip to the server pages are destroyed and re-created.
ASP.NET includes several options that help to preserve data on both a
per-page basis and an application-wide basis.
Client-Side
Method State Management
1. View state 2.Control state 3.Hidden fields 4.Cookies 5.
Query string
Server-Side
Method State Management
1. Application state 2.Session state 3.Profile properties 4.
Database support
State
management in Silverlight 2 can be done using the concept of Isolated storage. In Silverlight
application everything is compiled in one XAP file and when a user requests this
is copied to the client machine. So there are no server round trips and page
request every time, this reduces the need of maintaining the use or application
state. But in typical business application state management may be
required.
Silverlight
Isolated Storage
Isolated storage is a data storage mechanism that helps in storing data
on the client machine in a hidden folder outside the browser’s cache.
Silverlight Applications runs as partial trust but isolated storage API is
allowed to create and access files that are stored in an Isolated Storage area
which act as non volatile cache and is shared across the browsers. Every
application has its own portion of the isolated storage with a default size of
1MB and can be used to store user specific information as name, preferences and
skins etc. It can be used as Client-Side
state management.
Demo
Location of Isolated
Storage in
Vista
%:\Users\%\AppData\LocalLow\Microsoft\Silverlight\is
Location of Isolated
Storage in Windows XP
%:\Documents and Settings\%\Local
Settings\Application Data\Microsoft\Silverlight\is
Retrieve
Isolated Storage Information (Quota, Available Space)
private void ButtonShowInfo_Click(object sender, RoutedEventArgs e)
{
try
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
string spaceUsed = (store.Quota -
store.AvailableFreeSpace).ToString();
string spaceAvailable =
store.AvailableFreeSpace.ToString();
string currentQuota =
store.Quota.ToString();
string message = String.Format("Quota:
{0} bytes, Used: {1} bytes, Available: {2} bytes", currentQuota,
spaceUsed, spaceAvailable);
TextBoxResults.Text = message;
SaveLogToIsolatedStorage(message);
}
}
catch (IsolatedStorageException)
{
TextBoxResults.Text = "Unable to access
Isolated Storage.";
SaveLogToIsolatedStorage("Unable to access
Isolated Storage.");
}
}
Create a
variable for IsolatedStorageSettings
//Create
a variable for IsolatedStorageSettings
//IsolatedStorageSettings
contains the contents of the application IsolatedStorageFile scoped at the
application level
private IsolatedStorageSettings isoStorSettings = IsolatedStorageSettings.ApplicationSettings;
Save
information in Isolated Storage
private void ButtonSave_Click(object sender, RoutedEventArgs e)
{
try
{
User user = new User();
user.Name = TextBoxName.Text;
user.Designation = TextBoxDesignation.Text;
user.Address = TextBoxAddress.Text;
user.City = TextBoxCity.Text;
isoStorSettings.Add("UserDetail",
user);
TextBoxResults.Text = "User Detaild
stored.";
SaveLogToIsolatedStorage("User Detaild
stored.");
}
catch (ArgumentException ex)
{
TextBoxResults.Text = ex.Message;
SaveLogToIsolatedStorage(ex.Message);
}
}
Retrieve information from Isolated Storage
private void ButtonRetrieve_Click(object sender, RoutedEventArgs e)
{
try
{
User user = (User)isoStorSettings["UserDetail"];
TextBoxName.Text = user.Name;
TextBoxDesignation.Text = user.Designation;
TextBoxAddress.Text = user.Address;
TextBoxCity.Text = user.City;
TextBoxResults.Text = "User Details
retrieved.";
SaveLogToIsolatedStorage("User Details
retrieved.");
}
catch (System.Collections.Generic.KeyNotFoundException ex)
{
TextBoxResults.Text = ex.Message;
SaveLogToIsolatedStorage(ex.Message);
ClearTextBoxes();
}
}
Delete from
Isolated Storage
private void ButtonDelete_Click(object sender, RoutedEventArgs e)
{
isoStorSettings.Remove("UserDetail");
TextBoxResults.Text = "User Detaild
deleted.";
SaveLogToIsolatedStorage("User Detaild
deleted.");
ClearTextBoxes();
}
Increase the
Isolated Storage space
private void ButtonIncreaseAllocation_Click(object sender, RoutedEventArgs e)
{
try
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
// Request more space in
bytes.
Int64 spaceToAdd = 1 * 1024 *
1024;
Int64 currentAvailable =
store.AvailableFreeSpace;
// If available space is less than requested, then
increase.
if (currentAvailable <
spaceToAdd)
{
// Request more quota
space.
if (!store.IncreaseQuotaTo(store.Quota +
spaceToAdd))
{
// The user clicked NO to the host's prompt to
approve the quota increase.
TextBoxResults.Text = "User declined to
approve Quota inrease";
SaveLogToIsolatedStorage("User declined to approve Quota
inrease");
}
else
{
// The user clicked YES to the host's prompt
to approve the quota increase.
TextBoxResults.Text = "Quota
inreased";
SaveLogToIsolatedStorage("Quota
inreased");
}
}
else
{
string messge = String.Format("{0:0.00} MB is still available.", (decimal)currentAvailable / 1024 /
1024);
TextBoxResults.Text = messge;
SaveLogToIsolatedStorage(messge);
}
}
}
catch (IsolatedStorageException ex)
{
TextBoxResults.Text = ex.Message;
SaveLogToIsolatedStorage(ex.Message);
}
}
Isolated Storage space also let applications to read/write files without special permissions. The
IsolatedStorageFileStream class provides methods to access files in the Isolated
Storage and performs operations on them.
Save data in
the Isolated Storage File
private void SaveLogToIsolatedStorage(string message)
{
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isoStream =
isoStore.OpenFile("Log.txt", FileMode.Append, FileAccess.Write))
{
using (StreamWriter writer = new StreamWriter(isoStream))
{
writer.WriteLine(message);
}
}
}
}
Read data from
isolated Storage File
private void ButtonReadLog_Click(object sender, RoutedEventArgs e)
{
TextBoxResults.Text = ReadLogFromIsolatedStorage();
}
private string
ReadLogFromIsolatedStorage()
{
string content = string.Empty;
try
{
using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isoStream
=
new IsolatedStorageFileStream("Log.txt", FileMode.Open, isoFile))
{
using (StreamReader sr = new StreamReader(isoStream))
{
content = sr.ReadToEnd();
}
}
}
}
catch (IsolatedStorageException ex)
{
TextBoxResults.Text = ex.Message;
return "";
}
return content;
}
Further if you want any information to be globally available to all the
users (Server-Side State Management)
then one of the options is Application Resource. Anything added to the
Application resource acts similar to Application State in ASP.NET (information
will same throughout the application for all users).
Add object to the App
Resource (in App.xaml.cs)
private
void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new Page();
User user = new User();
user.Name = "Nipun
Tomar";
user.Designation = "Project
Lead";
user.Address = "";
user.City = "Philadelphia";
App.Current.Resources.Add("UserInfo", user);
}
Retrieve
Information from App Resource (in Page.xaml.cs)
private void ButtonAppState_Click(object sender, RoutedEventArgs e)
{
try
{
User user = (User)App.Current.Resources["UserInfo"];
TextBoxName.Text
= user.Name;
TextBoxDesignation.Text = user.Designation;
TextBoxAddress.Text = user.Address;
TextBoxCity.Text = user.City;
TextBoxResults.Text = "User Details
retrieved from Application Resources.";
SaveLogToIsolatedStorage("User Details
retrieved from Application Resources.");
}
catch (Exception ex)
{
TextBoxResults.Text = ex.Message;
SaveLogToIsolatedStorage(ex.Message);
ClearTextBoxes();
}
}