In this article, we will know what is State Management in ASP.NET and how it works, in this article we will keep our full attention on Client-side State Management.
What is State? (What is State)
as we all knowhandjob browser usually stateless There are. Now the question arises here that stateless what does it actually mean?
stateless It meanshandjob whenever we visit a websitehandjob So our browser does our requested functionality (requested functionality) or request (request) communication to the respective server based on (communicate) does. Browser HTTP Or HTTPS protocol communication to the respective server by using (communicate) does.
But that response (response) afterhandjob What will happen next? or when we use our web browser (web browser) What will happen if you go to that website again after closing ?
in this situation HTTP/HTTPs does not remember which website or URL went onhandjob Or in other words we can say that this is the original website of the previous website. state does not keephandjob What we saw before closing our browserhandjob whom stateless is called.
Now perhaps you must have understood what is meant by state and stateless. So we can say that our web browsers are stateless.
State Outline
as i said in the beginning, HTTP One stateless is protocol. So we can say that this is all the previously used requests (request) to service (service) All resources/contexts that provide (resources/references) removes.
These resources (resources) can be of many typeshandjob As–
- objects (Objects)
- allocated memory (Allocated Memory
- Sessions ID’s
- Some URL Information (some URL info)
State Management Types of State Management
ASP.NET in the following 2 State Management Methods are:
also fall
Client-Side State Management
Whenever we do client-side state management (Client-Side State Management) make use ofhandjob So state The related information is stored directly on the client-side. This information is provided on every request made by the user (request) and then after communicating from the server side, the user will respond (response) will provide
Its architecture is as follows:-
Server-Side State Management
Server-side state management is different from client-side state management but the operations and working are somewhat similar in functionality. In server-side state management, all the information is stored in the memory of the web server. Because of this special functionality, there are more secure domains on the server side than on client-side state management.
State Management Scenario (State Management Scenario)
It is a bit difficult to know directly which of the client-side and server-side architecture would be better to use for our application. We cannot say directly which architecture we will use client-side or server-side for state management.
State Management Techniques
State Management Techniques are based on client side and server side. Their functionality varies depending on the change in the state, hence the hierarchy is given here.
Client-side Techniques
Following are the client-side state management techniques,
- View State
- hidden field
- Cookies
- Control State
- Query Strings
Server-side Technique
Following are the server-side state management techniques,
- Session State
- Application State
Now we will know what each technique is and try to understand them with examples.
View State
In general we can say that View State Used to store user data in ASP.NET, sometimes called users in ASP.NET applications post-back VIEW STATE is most commonly used for this task after one wants to retain or store its data temporarily.
This property is enabled by default, but user can change it based on their functionality, all we have to do is use TRUE to enable ViewState value and FALSE option to disable it. will be
// Page Load Event protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { if (ViewState["count"] != null) { int ViewstateVal = Convert.ToInt32(ViewState["count"]) + 1; View.Text = ViewstateVal.ToString(); ViewState["count"]=ViewstateVal.ToString(); } else { ViewState["count"] = "1"; } } } // Click Event protected void Submit(object sender, EventArgs e) { View.Text=ViewState["count"].ToString(); }
Important things to be noted
View State Some of the features are as follows:-
- This is page-level State Management.
- View state is used to hold data temporarily.
- Any type of data can be stored in it.
- The view state is dependent on the property.
Hidden Field
Hidden fields are used to store small amounts of data on the client side. In simple words it is just a container of some objects, but whatever the result is it cannot be rendered on the web browser. Its result is invisible in the browser.
It stores a value for a single variable and it is a good method when the value of a variable can be changed frequently and we do not need to keep track of it again and again in our application or web program.
For example, suppose you have created a web page which has a form, along with this form you want to send such information to the next page, which you do not want to show to the user, but it is necessary to send this information along with the form. At this time we can use Hidden Field.
int newVal = Convert.ToInt32(HiddenField1.Value) + 1; HiddenField1.Value = newVal.ToString(); Label2.Text = HiddenField1.Value;
Important things to be noted
Following are some of the features of hidden fields:
- Hidden field has very small amount of memory
- Its functionality can be accessed directly.
cookies (Cookies
A set of cookies is a small text file that is saved to the user’s hard drive using the client’s browser. Cookies are used only for identity matching of the user as it only stores information such as session id, frequent navigation and post-back request objects. Is.
Whenever we connect to the Internet for a specific purpose, a cookie file is accessed from our hard drive through our browser to identify the user.
int postbacks = 0; if (Request.Cookies["number"] != null) { postbacks = Convert.ToInt32(Request.Cookies["number"].Value) + 1; } // Generating Response else { postbacks = 1; } Response.Cookies["number"].Value = postbacks.ToString(); Result.Text = Response.Cookies["number"].Value;
things to keep in mind
Following are some of the characteristics of cookies:
- It stores information temporarily
- This is a simple small size text file
- This can be changed depending on the requirements
- It is liked by the user.
- Creating cookies requires only a few bytes or KB of space.
Query String
Query string is used to fulfill a particular purpose. Usually they are used to place some values on a page and move these values to other pages. The information stored in it can be easily navigated from one page to another or even on the same page.
if (Request.QueryString["number"] != null) { View.Text = Request.QueryString["number"]; } // Setting query string int postbacks = 0; if (Request.QueryString["number"] != null) { postbacks = Convert.ToInt32(Request.QueryString["number"]) + 1; } else { postbacks = 1; } Response.Redirect("default.aspx?number=" + postbacks);
Things to note:
Some of its other features are as follows,
- It is usually used to hold the value
- it works temporarily
- Switches information from one page to another
- This leads to an increase in performance
- Uses real and virtual path values for URL routing.