0% found this document useful (0 votes)
6 views

State Management in ASP

Dot net management system

Uploaded by

bhavika M
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

State Management in ASP

Dot net management system

Uploaded by

bhavika M
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

State management in ASP.

NET involves techniques to preserve data across postbacks or


between requests. Here’s a detailed explanation of each state management technique with code
examples to illustrate how they work:

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;
}

protected void Page_Load(object sender, EventArgs e)


{
if (ViewState["SavedText"] != null)
{
TextBox1.Text = ViewState["SavedText"].ToString();
}
}
}
}

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:

<%@ 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>Application State Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" />
</form>
</body>
</html>

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;
}

Label1.Text = "This page has been accessed " +


Application["AppCounter"].ToString() + " times.";
}
}
}

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);
}

protected void Page_Load(object sender, EventArgs e)


{
HttpCookie cookie = Request.Cookies["UserText"];
if (cookie != null)
{
TextBox1.Text = cookie.Value;
}
}
}
}

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.

You might also like