Client Side State Management in ASP.NET
State Management in done on client side as well as on server side in ASP.NET. In this article, we will just focus on clinet side state management.
Client Side State Management
1. Cookies
2. Hidden Field
3. View State
4. Query Strings
2. Hidden Field
3. View State
4. Query Strings
Server Side State Management
1. Application object
2. Session object
3. Database Storage
2. Session object
3. Database Storage
Client Side State Management
1. Cookies: The main purpose of cookies is to identify users and possibly prepare customized Web pages for them. When you enter a Web site using cookies, you may be asked to fill out a form providing such information as your name and interests. This information is packaged into a cookie and sent to your Web browser which stores it for later use. The next time you go to the same Web site, your browser will send the cookie to the Web server. The server can use this information to present you with custom Web pages. So, for example, instead of seeing just a generic welcome page you might see a welcome page with your name on it.
[Delphi Prism]
Displaying:
if(Request.Cookies[“username”] <> null) then
begin
lblMessage.text := Request.Cookies[“username”].Value + ”, Welcome to World!”;
end;
begin
lblMessage.text := Request.Cookies[“username”].Value + ”, Welcome to World!”;
end;
Storing:
Response.Cookies[“username’].Value := username;
2. Viewstate and Hidden Fields:
Stores your data in hidden fields on postbacks. Viewstate is used to retain data when the page is posted back.
[Delphi Prism]
Storing: ViewState.Add(“myVar”,”Delphi Prism”);
Retrieving: myVar2 := ViewState[“myVar”].ToString();
3. Querystring: The portion of a dynamic URL that contains the search parameters when a dynamic Web site is searched. Query strings typically contain ? and & characters.
3. Querystring: The portion of a dynamic URL that contains the search parameters when a dynamic Web site is searched. Query strings typically contain ? and & characters.
[Delphi Prism]
Storing: http://www.mySite.com/login.aspx?username=abc
Or Response.Redirect(login.aspx? username=abc&role=xyz);
Or Server.Transfer(login.aspx? username=abc&role=xyz);
Or Response.Redirect(login.aspx? username=abc&role=xyz);
Or Server.Transfer(login.aspx? username=abc&role=xyz);
Retreiving: username2:= Request.Querystring[‘username’].ToString();
When to use what:
Cookie: You need to store small amounts of information on the client and security is not an issue.
Viewstate: You need to store small amounts of information for a page that will post back to itself. Use of the ViewState property does supply semi-secure functionality and better Data management
Querystring: You are transferring small amounts of information from one page to another and security is not an issue.
Note: You can use query strings only if you are requesting the same page, or another page via a link.