Skip to main content

How To store View State on the Server(asp.net 2.0)

In ASP.NET pages, the view state represents the state of the page when it was last processed on the server. It's used to build a call context and retain values across two successive requests for the same page. By default, the state is persisted on the client using a hidden field added to the page and is restored on the server before the page request is processed. The view state travels back and forth with the page itself, but does not represent or contain any information that's relevant to client-side page display. In this post i will show how to store viewstate on server side. For more details check out this link.
<%@ Page Language="C#" EnableViewState="true" AutoEventWireup="true" CodeFile="ViewStateOnServer.aspx.cs"
   Inherits="CropImage" %>

<!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 runat="server">
   <title>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
       <div>
           <div>
               <asp:GridView ID="GridView1" runat="server">
               </asp:GridView>
               <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="PostBack" /></div>
       </div>
   </form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;

public partial class CropImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["strTemp"] != null)
            {

                GridView1.DataSource = Session["strTemp"] as DataTable;

                GridView1.DataBind();


            }
            else
            {
                GridView1.DataSource = GetCustomMadeDataTable();


                GridView1.DataBind();
            }
        }

    }
   
    protected override object LoadPageStateFromPersistenceMedium()
    {
        return Session["_ViewState"];
    }
   

    protected override void SavePageStateToPersistenceMedium(object viewState)
    {
        Session["_ViewState"] = viewState;
    }
    public DataTable GetCustomMadeDataTable()
    {

        //Create a new DataTable object

        System.Data.DataTable objDataTable = new System.Data.DataTable();

        //Create three columns with string as their type

        objDataTable.Columns.Add("Id", typeof(string));
        objDataTable.Columns.Add("Column1", typeof(string));
        objDataTable.Columns.Add("Column2", typeof(string));
        objDataTable.Columns.Add("Column3", typeof(string));


        //Adding some data in the rows of this DataTable
        DataRow dr;
        for (int i = 0; i <= 20; i++)
        {
            dr = objDataTable.NewRow();
            dr[0] = i.ToString();
            dr[1] = "Column1Data" + i.ToString();
            dr[2] = "Column2Data" + i.ToString();
            dr[3] = "Column3Data" + i.ToString();
            objDataTable.Rows.Add(dr);
        } DataColumn[] dcPk = new DataColumn[1];
        dcPk[0] = objDataTable.Columns["Id"];
        objDataTable.PrimaryKey = dcPk;
        Session["strTemp"] = objDataTable;
        return objDataTable;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    }
}

Comments

Popular posts from this blog