State management in asp.net, first part: cookies
Sometimes it is necessary to receive useful information and use it on other pages. There are several ways to do this; one of these methods is using cookies. Now, in this article, we will discuss how to transfer variables and data in two steps: 1- Creating a cookie and placing variables and data in asp.net and 2- Retrieving variables and data from cookies.
State Management refers to a set of solutions to maintain the status/mode of variables and program data in transferring from one page to another.
In the first part, we will introduce the options of saving information on the page or the user’s (client) computer. In the options of this section, no information is saved on the server.
1) View State: This property provides a list-like object for storing information between multiple requests to send and receive the page to the server, for that page. This object is the default method of saving page information and the controls in it between round trips to the server. When the page is sent to the server, the current state of the page and the information in its controls are stored in the form of an encrypted text and inside one or more Hidden Field controls. If the amount of stored information exceeds the value set in the MaxStateFieldLength property, the page will use multiple Hidden Field controls. When the page returns from the server (Postback), it reads the information in the View State text object and returns them to the related controls.
2) Control State: Sometimes you need to store the status information of a particular control at a certain point for its proper functioning. For example, suppose that you have designed a custom control that has different tabs that store different information. For example, in the process of going back and forth between the page and the server for this particular control, you need to know which edge the information control shows before sending the page to the server. You can use the View State feature to maintain the state of this control, but this feature can be disabled on the page and may be accidentally or intentionally turned off and cause the functionality of the above control to fail. To solve this problem, Asp.Net has provided the use of the Control State feature. With the Control State object, you can save the current state of a control on the page before sending it to the server, without the possibility of disabling it.
3) Hidden Fields: Asp.Net language allows you to store your information in hidden fields or HiddenField controls. These controls are rendered as hidden field input tags on Html pages. The Hidden Fields control has no visible output on the screen, but like other Asp.Net controls, you can set the desired properties for it. When the page is sent to the server, the value of a Hidden Fields control is sent along with the values of other page controls through an HTTP Form protocol. The Hidden Fields control stores information in its Value property and can be determined directly.
Note 1: If you want a Hidden Fields control to function correctly and its information to be accessible during page Postback, you must send the page to the server using an HTTP Post method. If you send the page to the server by clicking on a link or through the HTTP GET method, this control will not work.
Note 2: Although the Hidden Fields control information is not visible on the page, it can be seen by other users by viewing the page code (Source Code). Therefore, special information should not be included in this control.
4) Cookies: A cookie is a small piece of information that is stored in a text file or in the browser’s memory. Cookies can have an expiration date or an unlimited time. You can use cookies to store user information or software, etc. Cookies are stored on the user’s computer and when the browser requests a page, it sends the cookie of that page to the server along with its request. The server can read and use cookies.
In an article titled what are cookies and how to use cookies in PHP , we have talked about this in detail.
5) Query String: The next object stored in memory is query strings. Query Strings are information that is appended to the continuation of the page address.
The URL above has two Query Strings named Category and Price with values determined by the sign? They are added at the end of the page address. Usually, Query String is used to transfer information between pages and has a limited volume for sending information. Also, this information can be seen by the user and has very low security. To send information through Query String, you must send the page using the HTTP GET method, so when using the HTTP POST method, these objects do not work.
Server-side status management options:
Asp.Net has provided the designer with various options to store information and page status on the server instead of storing them on the user’s computer. By using the information storage facilities on the server, you can reduce the amount of information sent from the status and page data to the server, but on the other hand, you will use more of its hardware resources.
1) Application State: The Asp.Net language allows you to maintain values for any active web application through the Application State feature, which is a version of the HTTP Application State class. Application State is a global memory that can be accessed through all pages of the web application. Therefore, memory is suitable for storing general information and information that must be used every time pages are sent to the server. Application State is created in name/value pairs for each request to a specific URL.
2) Session State: The Asp.Net language has provided us with another option to store information in memory called Session State, which is a version of the HTTP Session State class. Session State is very similar to Application State, except that it focuses on the state of the current browser. If multiple users use your web application at the same time, each user will have their own version of Session. Also, if the user abandons the use of the web application and returns after a while, the first session will be different from the second session. Session State is also used as a pair of name/value values to store information that is needed every time the page is sent to the server. In general, this feature can be used for the following:
- Identifying the browser or client computer that sent the request to the server and sending the request to the relevant information in the server’s memory.
- Keeping the desired information on the server for use in several browsers or clients in a single session.
- Enable events designed for session management.
3) Profile Properties: With Profile Properties, Asp.Net can store user specific information. This feature is very similar to Session State, except that the Profile Properties information is not lost when the Session is lost. The possibility of Profile Properties uses an Asp.Net profile with a predetermined format that is different for each user. The Asp.Net profile option provides you with this feature so that you can store user information without designing a database from scratch. To use the possibility of Profile Properties in your application, you must activate a Profile provider in your application. Asp.Net has a SQL Profile Provider class that you can use to store data in a SQL database or create a data source with your desired format and structure, such as an SQL database or an XML file.
Ways to transfer variables and data from one page to another using cookie
First step: Creating a cookie and putting variables and data in it
For this purpose, you must first use the Response. Cookies command to create a cookie. Note that this command must be used before the <html> tag. For example, using the following command, we create two cookies, each of which has a variable called age and the value is 27 and a variable called firstname with the value of sajad.
<%
Response.Cookies(“firstname”)=”sajad”
Response.Cookies(“age”)=”27″
%>
Because cookies are stored on the visitor’s local computer, you can apply some settings for cookies. For example, you can specify how long a cookie will remain on the visitor’s local computer, for example, in the code below, this date is specified as May 10, 2012.
<%
Response.Cookies(“firstname”)=”sajad”
Response.Cookies(“firstname”).Expires=#May 10,2015#
Response.Cookies(“age”)=”۲۷″
Response.Cookies(“age “).Expires=#May 10,2012#
%>
Second step: Retrieving variables and data from cookies
Variables and data can be retrieved from cookies using the Request. Cookies command. For example, using the following commands, you can restore the firstname and age variables that we created above and the given value.
<%
fname=Request.Cookies(“firstname”)
response.write(“your first name is= ” & fname)
age=Request.Cookies(“age”)
response.write(“your age is= ” & age)
%>
In ASP.Net, there are two ways to write a cookie to the user’s machine:
Determining cookie characteristics in the cookies collection
Creating an instance of the HttpCookie object and sending it to the cookies collection
Important note: Cookies must be defined and set before the output of the ASP.Net page is created or rendered, for example, it must be defined in the Page_Load event of the page and not in a function such as Page_Unload.