State Management in ASP
State Management in ASP
1. View State
Explanation: View State is used to store the state of web page controls between postbacks. It’s
stored as a hidden field in the page and is sent back and forth between the client and server.
Example:
Default.aspx:
asp
Copy code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="YourNamespace.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>View State Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Save"
OnClick="Button1_Click" />
</form>
</body>
</html>
Default.aspx.cs:
csharp
Copy code
using System;
namespace YourNamespace
{
public partial class Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
// Save data to ViewState
ViewState["SavedText"] = TextBox1.Text;
}
Explanation:
The ViewState dictionary stores data in a hidden field on the page. When Button1 is
clicked, the text from TextBox1 is saved to ViewState. On subsequent page loads
(postbacks), the saved value is retrieved and displayed in TextBox1.
2. Session State
Explanation: Session State allows you to store user-specific data on the server for the duration
of the user’s session. It’s suitable for storing user-specific data across multiple pages.
Example:
Default.aspx:
asp
Copy code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="YourNamespace.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Session State Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Save"
OnClick="Button1_Click" />
</form>
</body>
</html>
Default.aspx.cs:
csharp
Copy code
using System;
namespace YourNamespace
{
public partial class Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
// Save data to Session
Session["SavedText"] = TextBox1.Text;
}
protected void Page_Load(object sender, EventArgs e)
{
if (Session["SavedText"] != null)
{
TextBox1.Text = Session["SavedText"].ToString();
}
}
}
}
Explanation:
Data stored in Session is preserved for the duration of the user’s session. When Button1
is clicked, the text from TextBox1 is saved to Session. On subsequent page loads, the
saved value is retrieved from Session and displayed in TextBox1.
3. Application State
Explanation: Application State stores global data that is shared across all users and sessions. It’s
useful for storing application-wide settings or counters.
Example:
Default.aspx:
Default.aspx.cs:
using System;
namespace YourNamespace
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Application["AppCounter"] == null)
{
Application["AppCounter"] = 1;
}
else
{
Application["AppCounter"] = (int)Application["AppCounter"] +
1;
}
Explanation:
Application state is used to store data that needs to be shared across all users. In this
example, a counter keeps track of how many times the page has been accessed.
4. Cookies
Explanation: Cookies are small pieces of data stored on the client’s browser. They can be used
to store user preferences or tracking information.
Example:
Default.aspx:
asp
Copy code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="YourNamespace.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Cookies Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Save"
OnClick="Button1_Click" />
</form>
</body>
</html>
Default.aspx.cs:
csharp
Copy code
using System;
namespace YourNamespace
{
public partial class Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
// Save data to Cookie
HttpCookie cookie = new HttpCookie("UserText");
cookie.Value = TextBox1.Text;
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
}
Explanation:
Cookies are sent to the client’s browser and are used to store information such as user
preferences. When Button1 is clicked, the value of TextBox1 is stored in a cookie. On
subsequent page loads, the cookie value is retrieved and used to populate TextBox1.
5. Query Strings
Explanation: Query Strings are used to pass data in the URL. This method is useful for
transferring small amounts of data between pages but can expose sensitive information.
Example:
Page1.aspx:
asp
Copy code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page1.aspx.cs"
Inherits="YourNamespace.Page1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Query Strings Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Send to Page2"
OnClick="Button1_Click" />
</form>
</body>
</html>
Page1.aspx.cs:
csharp
Copy code
using System;
namespace YourNamespace
{
public partial class Page1 : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
// Pass data using Query String
Response.Redirect("Page2.aspx?Text=" +
Server.UrlEncode(TextBox1.Text));
}
}
}
Page2.aspx:
asp
Copy code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page2.aspx.cs"
Inherits="YourNamespace.Page2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Query Strings Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server" />
</form>
</body>
</html>
Page2.aspx.cs:
csharp
Copy code
using System;
namespace YourNamespace
{
public partial class Page2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["Text"] != null)
{
TextBox1.Text = Request.QueryString["Text"];
}
}
}
}
Explanation:
Data is passed between pages using query strings in the URL. In Page1, when the button
is clicked, it redirects to Page2 with the data appended to the URL. Page2 retrieves this
data from the query string and displays it in TextBox1.