State Management in ASP.
NET
Aim
To implement state management techniques such as ViewState, Session, and Cookies in an
ASP.NET web application.
Theory:
State management allows maintaining user data across multiple web requests in a web
application.
There are two types of state management: client-side and server-side.
Techniques used in this experiment:
1. View State (Client-side)- Retains values on the same page
2. Session (Server-side) - Stores user-specific data across pages
3. Cookies (Client-side) - Stores small data in the user's browser.
VIEW STATE
Step-by-Step Procedure:
1. Open Visual Studio and create a new ASP.NET Web Forms Application.
2. Add a new Web Form and name it 'StateDemo.aspx'.
3.StateDemo.aspx (Design)
<asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="BtnSaveViewState" runat="server" Text="Save ViewState"
OnClick="BtnSaveViewState_Click" />
<asp:Label ID="LblViewState" runat="server" />
4.Edit the following code in click event of button
protected void BtnSaveViewState_Click(object sender, EventArgs e)
ViewState["MyData"] = TextBox1.Text;
LblViewState.Text = "ViewState Data: " + ViewState["MyData"];
}
Session State:
Step-by-Step Procedure:
Code Behind (SessionPage1.aspx.cs):
1. Open your ASP.NET Web Forms project in Visual Studio.
2. Right-click the project name and select Add > Web Form.
3. Create two pages:
- SessionPage1.aspx
- SessionPage2.aspx
Design for SessionPage1.aspx:
<asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="BtnSetSession" runat="server" Text="Set Session & Go to Page 2"
OnClick="BtnSetSession_Click" />
Code Behind (SessionPage1.aspx.cs):
protected void BtnSetSession_Click(object sender, EventArgs e)
Session["UserName"] = TextBox1.Text;
Response.Redirect("SessionPage2.aspx");
Design for SessionPage2.aspx:
<asp:Label ID="LblSessionData" runat="server" />
Code Behind (SessionPage2.aspx.cs):
protected void Page_Load(object sender, EventArgs e)
if (Session["UserName"] != null)
{
LblSessionData.Text = "Session value from Page 1: " + Session["UserName"].ToString();
else
LblSessionData.Text = "No session value found.";
How to Set Start Page:
1. Right-click SessionPage1.aspx in Solution Explorer.
2. Click 'Set as Start Page'.
COOKIES
1.Open your ASP.NET Web Forms project in Visual Studio.
2.Right-click the project name and select Add > Web Form.
3. Default.aspx coding:
<asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="BtnSaveCookie" runat="server" Text="Save Cookie"
OnClick="BtnSaveCookie_Click" />
<asp:Label ID="LblCookie" runat="server" />
4. Edit the following code in click event of the button
protected void BtnSaveCookie_Click(object sender, EventArgs e)
HttpCookie cookie = new HttpCookie("MyCookie");
cookie.Value = TextBox1.Text;
cookie.Expires = DateTime.Now.AddMinutes(5);
Response.Cookies.Add(cookie);
if (Request.Cookies["MyCookie"] != null)
LblCookie.Text = "Cookie Data: " + Request.Cookies["MyCookie"].Value;
Result:
This the ViewState, Session, and Cookies manage user data in ASP.NET Web Forms applications
was executed successfully.