Before diving into state management in asp net, let us first briefly discuss what is meant by ASP NET and its uses.
ASP NET and Its Use
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.
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.
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 -
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));
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
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!
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 knowing C# further in order to become a web and desktop application developer, Simplilearn offers an exclusive full-stack web development certification course 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!