ASP stands for Active Server Pages. It is developed by Microsoft and is a free, Open-Sourced server-side framework for web applications designed to build websites that are great and to enable the creation of dynamic pages, Web APIs, and usage of real-time technologies like Web Sockets.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Are ASP.NET and .NET the Same? 

No, ASP NET is a framework for web applications to enable the creation of dynamic web pages, whereas .NET is a platform for development where one can develop, compile, run and execute the applications.

Now, let us discuss what is meant by state management.

State Management in General Terms

It refers to managing the state of one or several user interface control systems like that of radio controls, action buttons, input fields, etc. It is a programming technique for User Interface in which the state of a single UI control completely or partially depends on the state of all the other UI controls.

Also Read: How to Learn Programming?

State Management in ASP.NET

In an ASP NET application, state management in ASP NET is an object and preserves type state control. This is because ASP NET applications are basically stateless. In ASP NET, the information of users is stored and maintained till the user session ends. Each time the page is posted on the server, a new instance of the Web page class is created. Whenever the user enters information, this information might get lost in the round trip from the browser (MSDN), if they enter into the web application. 

Now, let’s discuss the types of State Management.

Types of State Management in ASP.NET 

There are two types of State management in ASP net. They are :

  • Server-side 
  • Client-side

These are further subdivided into the following -

Server-Side

  • Session
  • Application
  • Cache

Client-Side

  • Cookies 
  • Viewstate
  • Control state
  • Query String
  • Hidden Field 

Let us see in detail how these types of state management in ASP NET work -

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

State Management in ASP.NET Techniques

Server-Side of State Management in ASP NET

Session 

An important technique to maintain state. It is used to store identity and information; information is stored in the server using Sessionid.

To start the user session -

Example

protected void btnSubmit_Click(object sender, EventArgs e)  

{  

   Session["UserName"] = txtName.Text;  

   Response.Redirect("Home.aspx");  

}  

Session Event

Two types -

Session starts - Raised every time a new user requests without a session ID.

Example

void Session_Start(object sender, EventArgs e)  

{  

   Session["Master"] = "~/Master.master";  

}  

Session end - Raised everytime the user ends the session or a time out occurs.

Example

void Session_End(object sender, EventArgs e)  

{  

   Response.Write("Session_End");  

}  

The session is stored in the following ways in ASP.NET:

InProcMode - Default session mode. When the server starts, the session value is stored, and when the server is restarted, it ends.

State Server Mode - Session date is made to store on a separate server in this mode.

SQL Server Mode - It’s a secure mode in which the session is made to store in the database.

Custom Mode - Session data is generally stored in InProcMode, SQL Server Mode, etc. In case we want to store using any other techniques, we use the custom mode.

Application

It is a server-side management state and is also known as the application level state management. This is mainly used to store user activity in server memory and application events.

This is further classified into three types :

Application start - The event begins with the start of the domain.

Example 

Void ApplicationStart(object sender, EventArgs e)  

{  

   Application["ApplicationstartMessage"] = "Welcome to Simplilearn";  

}  

Application error - This is used to manage/handle an error or exception that had been previously unhandled.

Example

void ApplicationError(object sender, EventArgs e)   

{   

   // Unhandled exception code block 

}

Application end - Whenever the domain ends, this ends as well.

Example 

Void ApplicationEnd(object sender, EventArgs e)  

{  

   Application["ApplicationEndMessage"] = "Applications are Closed";  

}

Cache

This is stored on the server-side, and it is used to implement page caching and data caching. A cache is primarily used to set expiration policies.

Example snippet

Response.Cache.SetExpiresTime(DateTime.Now.AddDays(1));

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Client-Side State Management in ASP.NET

There are four main parts of State Management on the Client Side -

Cookie

One of the smallest but important parts of the ASP NET, it is used to store the session and application information of the user. It can be constant and temporary and works with browser requests. The server can read these cookies from the client-side and perform data abstraction. 

There are two types of cookies that are available -

Persistence - The Persistence cookie works along with Time and Date.

Example 

Response.Cookies["CookiesName"].Value = "Testing Cookies";  

//setting the expire time

Response.Cookies["CookiesName"].Expires = DateTime.Today.AddHours(2);  

Non-Persistence - Temporary cookie created with application access and closed application is discarded.

Example 

Response.Cookies["CookiesName"].Value = "Testing Cookies";  

Control State

It is a technique used to maintain data work in order, properly. We use Control State to use the view state without the possibility of it being disabled by the user. 

Example 

if (!IsPostBack)  

{  

   lblmsg1.Text = "Welcome to Simplilearn!";  

   lblmsg2.Text = "Welcome to Simplilearn!";  

We use a customized control state to control the one being displayed when these two messages are being displayed on the PostBack event.

Hidden Field

This field is used to store values on the client-side. The hidden field works on request and is not displayed on the browser.

Example 

if (HiddenField.Value != null)  

{  

   int dat = Convert.ToInt32(HiddenField.Value) + 1;  

   HiddenField.Value = dat.ToString();  

   Labels.Text = dat.ToString();  

Viewstate

It is used to manage page-level state and is used for storing, sending, and receiving information.

We can store small values in Viewstate, but it is pretty easy to apply since it does not require any server resources. The page using Viewstate becomes heavy if more data is stored.

Example 

if (ViewState["User_Name"] != null)  

lblName.Text = ViewState["User_Name"].ToString();  

Query String

The query string is used to store the value in the URL.

Example 

Response.Redirect("ShowStringValue.aspx?Username=" + txtUsername.Text);

Output 

Query_String

Custom Session Mode

There are several custom session storage mechanisms that ASP.NET applications can use. Custom session state store providers enable developers to create their own session state storage mechanisms. This is the most flexible option, but also the most difficult to implement. Developers can also use a custom session state manager. This option is less flexible than using a custom session state store provider, but is easier to implement. Finally, developers can use a custom session ID generator. This option is the simplest to implement, but provides the least flexibility. Developers who want to use a custom session storage mechanism must first decide which option is best for their needs.

Sample Code and Examples

State management is a process by which you maintain the state of an object or variable throughout the lifetime of a page. In ASP.NET, there are two types of state management: client-side state management and server-side state management.

Client-side state management refers to the technique of storing data on the client's browser in the form of either cookies or hidden fields. Server-side state management, on the other hand, stores data on the server in the form of either application state or session state.

Application state is a global storage mechanism that is used to store data that needs to be available to all users of an ASP.NET application. Session state, on the other hand, is a per-user storage mechanism that is used to store data that is specific to a user's session.

Client-Side State Management

Cookies

One of the most common techniques for storing data on the client's browser is through the use of cookies. Cookies are small text files that are stored on the user's computer, and they can be used to store data such as user preferences, session information, and so on.

To create a cookie, you can use the Response.Cookies collection, as shown below:

Response.Cookies["userName"].Value = "John Smith";

Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);

In the code above, we are creating a cookie named "userName" and setting its value to "John Smith". We are also setting an expiration date for the cookie, so that it will be deleted from the user's computer after one day.

To retrieve a cookie, you can use the Request.Cookies collection, as shown below:

string userName = Request.Cookies["userName"].Value;

If you want to delete a cookie, you can set its value to an empty string and then expire it immediately, as shown below:

Response.Cookies["userName"].Value = "";

Response.Cookies["userName"].Expires = DateTime.Now.AddDays(-1);

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Hidden Fields

Another technique for storing data on the client's browser is through the use of hidden fields. Hidden fields are HTML input elements that are used to store data that is not meant to be seen or edited by the user.

To create a hidden field, you can use the following code:

<input type="hidden" name="userName" value="John Smith" />

To retrieve the value of a hidden field, you can use the Request.Form collection, as shown below:

string userName = Request.Form["userName"];

ViewState

ViewState is a technique that is used to store the state of a page between postbacks. ViewState is stored in a hidden field on the page, and it is encoded using base64 encoding.

ViewState can be used to store data such as user input, control state, and so on. To enable ViewState for a page, you can use the following code:

<%@ Page EnableViewState="true" %>

To access the ViewState of a page, you can use the following code:

string viewState = (string)ViewState["userName"];

To store data in the ViewState of a page, you can use the following code:

ViewState["userName"] = "John Smith";

Server-Side State Management

Application State

Application state is a global storage mechanism that is used to store data that needs to be available to all users of an ASP.NET application. Application state is stored in memory on the server, and it is specific to an ASP.NET application.

To access the application state, you can use the following code:

string appState = (string)Application["userName"];

To store data in the application state, you can use the following code:

Application["userName"] = "John Smith";

Session State

Session state is a per-user storage mechanism that is used to store data that is specific to a user's session. Session state is stored in memory on the server, and it is specific to a user's session.

To access the session state, you can use the following code:

string sessionState = (string)Session["userName"];

To store data in the session state, you can use the following code:

Session["userName"] = "John Smith";

Profile Properties

Profile properties are a per-user storage mechanism that is used to store data that is specific to a user's profile. Profile properties are stored in a database, and they can be used to store data such as user preferences, contact information, and so on.

To access a profile property, you can use the following code:

string profileProperty = (string)Profile["userName"];

To store a value in a profile property, you can use the following code:

Profile["userName"] = "John Smith";

Cache

The cache is a global storage mechanism that is used to store data that needs to be available to all users of an ASP.NET application. The cache is stored in memory on the server, and it is specific to an ASP.NET application.

To access the cache, you can use the following code:

string cacheItem = (string)Cache["userName"];

To store data in the cache, you can use the following code:

Cache["userName"] = "John Smith";

Conclusion 

Statement Management in ASP NET is an important part of the complete web application development process using ASP NET. It is used to help us manage the state of one or several user interface control systems while building web applications.

To truly master the power of Statement Management in ASP NET and all its related topics, one might consider referring and learning in-depth from various resources, study materials, and course books.

If you are interested in learning C# further to become a web and desktop application developer, Simplilearn offers an exclusive Post Graduate Program in Full Stack Web Development to master both backend and frontend with tools, like SpringBoot, AngularMVC, JSPs, and Hibernate to start your career as a full stack developer.

Simplilearn also offers free online skill-up courses in several domains, from data science and business analytics to software development, AI, and machine learning. Become a full-stack web developer today!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 17 Jun, 2024

6 Months$ 8,000
Full Stack Developer - MERN Stack

Cohort Starts: 30 Apr, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 1 May, 2024

11 Months$ 1,499
Full Stack Java Developer

Cohort Starts: 14 May, 2024

6 Months$ 1,449