previous | Home | Next |
State management is the process by which you maintain state and page information over multiple requests for the same or different pages.
Types of State Management
Client – Side State Management
ASP.NET provides various client side state management options like Cookies, Query Strings (URL), Hidden fields, View State and Control state (ASP.NET 2.0).This stores information on the client's computer by embedding the information into a Web page, a uniform resource locator (url), or a cookie. The techniques available to store the state information at the client computer.
a. View State – Asp .Net uses View State to track the values in the Controls. You can add custom values to the view state. It is used by the Asp .net page framework to automatically save the values of the page and of each control just prior to rendering to the page. When the page is posted, one of the first tasks performed by page processing is to restore view state.
Example of View state
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>ViewState</title> </head> <body> <form id="form1" runat="server"> <asp:TextBox runat="server" id="NameField" /> <asp:Button runat="server" id="SubmitForm" onclick="SubmitForm_Click" text="Submit & set name" /> <asp:Button runat="server" id="RefreshPage" text="Just submit" /> <br /><br /> Name retrieved from ViewState: <asp:Label runat="server" id="NameLabel" /> </form> </body> </html>
.vb Code
Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If ViewState("NameOfUser") IsNot Nothing Then NameLabel.Text = ViewState("NameOfUser").ToString() Else NameLabel.Text = "Not set yet..." End If End Sub End Class
The main advantage of view state is persisting controls properties without any programming and memory will not be occupied on the client system or on the server system.
Disadvantage
The disadvantage can be considered as a lot amount of data transmission between client and server.
- Control State – If you create a custom control that requires view state to work properly, you should use control state to ensure other developers don’t break your control by disabling view state.
- Hidden fields – Like view state, hidden fields store data in an HTML form without displaying it in the user's browser. The data is available only when the form is processed.
- Cookies – Cookies store a value in the user's browser that the browser sends with every page request to the same server. Cookies are the best way to store state data that must be available for multiple Web pages on a web site.
- Query Strings - Query strings store values in the URL that are visible to the user. Use query strings when you want a user to be able to e-mail or instant message state data with a URL.
previous | Home | Next |