A day with .Net

My day to day experince in .net

State Management in ASP.NET-Part 1

Posted by vivekcek on July 28, 2009

State management in ASP.NET concerns with tracking the user state in the application. Two types of state management in ASP.NET are client-side state management and server-side state management.
In the case of client side state management data is stored in user’s browser cache, or in browser cookies. But in the case of server side state management state is stored in server memory or in a database.
Various techniques used in state management are.
1: View State
View state is the default mechanism used by asp.net to store user specific request response data between page requests.
The Page.ViewState property returns a dictionary object for storing values in multiple requests of the same page.
When an asp.net page is processed the state of the page and its controls is hashed into a string and stored in a hidden field named _VIEWSTATE.

<input Type=”Hidden” name=”_VIEWSTATE” id=”_VIEWSTATE” value=”/jkhsdjkahdjsh5765”/>

View state is not secure; view state can be encrypted by ViewStateEncryptionMode property of a page
This will affect performance due to encryption and decryption. The view state encryption for an entire page can be set by the following code in the web.config file

<Configuration>
</system.web>
<pages ViewStateEncryptionMode=”always”/>
</system.web>
<Configuration>

Adding data to view state


this.ViewState.Add(“key”,”mydata”);

Retrieve data from view state

String myData=(string)ViewState[“key”];

The following code demonstrates a page that uses view state. It allows the user to save a set of values
(all the text that’s displayed in all the text boxes of a table) and restore it later. This example uses
recursive logic to dig through all child controls, and it uses the control ID for the view state key,
because this is guaranteed to be unique in the page.

protected void Button1_Click(object sender, EventArgs e)
    {
        SaveAllText(TABLE1.Controls, true);
        txtName.Text = "";
        txtPhone.Text = "";
    }

    private void SaveAllText(ControlCollection controls, bool saveNested)
    {

        foreach (Control control in controls)
        {
            if (control is TextBox)
            {
                // Store the text using the unique control ID.
                ViewState[control.ID] = ((TextBox)control).Text;
            }
            if ((control.Controls != null) && saveNested)
            {
                SaveAllText(control.Controls, true);
            }
        }
    
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        RestoreAllText(TABLE1.Controls, true);
    }
    private void RestoreAllText(ControlCollection controls, bool saveNested)
    { 
       foreach (Control control in controls)
           {
               if (control is TextBox)
                  {
                     if (ViewState[control.ID] != null)
                             ((TextBox)control).Text = (string)ViewState[control.ID];
                  }
                if ((control.Controls != null) && saveNested)
                 {
                     RestoreAllText(control.Controls, true);
                 }
           }
     }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s