Skip to main content

Web Forms State Management Part1: Client-Based State Management Options

Web pages are recreated each time the page is posted to the server. For example, if a user enters information into a text box, that information would be lost in the round trip from the browser or client device to the server. To overcome this inherent limitation of traditional Web programming, the ASP.NET page framework includes various options to help you preserve changes — that is, for managing state. The page framework includes a facility called view state that automatically preserves property values of the page and all the controls on it between round trips. However, you will probably also have application-specific values that you want to preserve. To do so, you can use one of the state management options.Client-Based State Management Options: - Storing information either in the page or on the client computer.
1. View State: - When the page is processed, the current state of the page and controls is hashed into a string and saved in the page as a hidden field. When the page is posted back to the server, the page parses the view state string at page initialization and restores property information in the page.To store information in view state: - ViewState["Time"] = DateTime.Today.ToLongDateString();
To retrieve information from view state: - Response.Write( (string) ViewState["Time"]);The advantages of using view state are:
- No server resources required. The view state is contained in a structure within the page code.
- Simple implementation.
- Automatic retention of page and control state.
- Enhanced security features. The values in view state are hashed, compressed, and encoded for Unicode implementations, thus representing a higher state of security than hidden fields have.
The disadvantages of using the view state are:
- Performance. Because the view state is stored in the page itself, storing large values can cause the page to slow down when users display it and when they post it.
- Security. The view state is stored in a hidden field on the page. Although view state stores data in a hashed format, it can be tampered with. The information in the hidden field can be seen if the page output source is viewed directly, creating a potential security issue.
2. Hidden Form Fields: - A hidden field does not render visibly in the browser, but you can set its properties just as you can with a standard control. When a page is submitted to the server, the content of a hidden field is sent in the HTTP Form collection along with the values of other controls.
Note: If you use hidden fields you must submit your pages to the server using the HTTP POST method rather than requesting the page via the page URL (the HTTP GET method).
The advantages of using hidden fields are:
- No server resources are required. The hidden field is stored and read from the page.
- Broad support. Almost all browsers and client devices support forms with hidden fields.
- Simple implementation.
The disadvantages of using hidden fields are:
- Security: - The information in the hidden field can be seen if the page output source is viewed directly
- Limited storage structure. Hidden fields offer a single value field in which to place information. To store multiple values, you must implement delimited strings and the code to parse those strings.
- Performance. Storing large values can cause the page to slow down
3. Cookies: - Useful for storing small amounts of frequently changed information on the client.
Note: Cookies are often used for personalization, where content is customized for a known user. In most of these cases, identification is the issue rather than authentication, so it is enough to merely store the user name, account name, or a unique user ID (such as a GUID) in a cookie and use it to access the user personalization infrastructure of a site.
The advantages of using cookies are:
- No server resources are required. It is stored on the client.
- Simplicity. The cookie is a lightweight, text-based structure with simple key-value pairs.
- Configurable expiration. The cookie can expire when the browser session ends, or it can exist indefinitely on the client computer, subject to the expiration rules on the client.
The disadvantages of using cookies are:
- Limited size. Most browsers place a 4096-byte limit on the size of a cookie
- User-configured refusal. Some users disable their browser or client device's ability to receive cookies
- Security. Users can manipulate cookies on their computer
- Durability. The durability of the cookie on a client computer is subject to cookie expiration processes on the client and user intervention.

Set Cookies HttpCookie MyCookie = new HttpCookie("LastVisit");
DateTime now = DateTime.Now;

MyCookie.Value = now.ToString();
MyCookie.Expires = now.AddHours(1);

Response.Cookies.Add(MyCookie);

4. Query Strings: - is information appended to the end of a page's URL. They are an easy way to pass information from one page to another page where it will be processed. Note Query strings are a viable option only when a page is requested via its URL. You cannot read a query string from a page that has been submitted to the server.
The advantages of using query strings are:
- No server resources required.
- Broad support. Almost all browsers and client devices support passing values in a query string.
- Simple implementation.
The disadvantages of using query strings are:
- Security. The information in the query string is directly visible to the user via the browser user interface. - Limited capacity. Most browsers and client devices impose a 255-character limit on URL length.

Comments

Popular posts from this blog

ഗുരു ദേവോ ഭവ:

ഇ ന്ന് ദേശീയ അധ്യാപക ദിനം. അധ്യാപകനും രാഷ്ട്രപതിയുമായിരുന്ന ഡോ.എസ് രാധാകൃഷ്ണന്‍റെ പിറന്നാള്‍ ദിനമാണ് അധ്യാപക ദിനമായി നാം ആചരിക്കുന്നത്. ഈ ഒരു സുദിനത്തില്‍ ഈയൊരു അധ്യാപകനെ സ്മരിച്ചില്ലെങ്കില്‍ അതൊരു നന്ദികേടായി പോവും.  ഓര്‍മ്മക്കുറിപ്പിലേക്ക് കടക്കുന്നതിനു മുന്‍പൊരു ഡിസ്ക്ലെയിമര്‍. ബാദ്ധ്യതാ നിരാകരണം ഒടുക്കം മാത്രം നടത്തുന്നതാണ് ആചാരം. എങ്കിലും, അതു താനല്ലയോ ഇതെന്ന് ചുമ്മാ കല്‍പ്പിച്ചു കൂട്ടി എന്തിനുമേതിനും വ്രണപ്പെടുന്നൊരു സമൂഹത്തില്‍, അവകാശപരിത്യാഗം ആദ്യം തന്നെ നടത്തുകയെന്ന ആചാരലംഘനമാവും ഉചിതം. അതെ, ഇനി ഇവിടെ പറയാന്‍ പോവുന്നതൊരു മിത്ത് മാത്രമാണു. എന്ന്? എവിടെ? എത്രത്തോളം? നടന്നുവെന്നതിനൊന്നും ഇവിടെയൊരു പ്രസക്തിയില്ല. പതിറ്റാണ്ടുകളായി കേരളത്തിലെ പലപല കോളേജുകളും ഇതെന്‍റെ ഗര്‍ഭമാണെന്ന അവകാശവാദവുമായി എത്തിയിട്ടുണ്ടു. സത്യം ആര്‍ക്കറിയാം! ഒരു കാര്യം മാത്രം എനിക്കു തറപ്പിച്ചു പറയാം, എന്‍റെ ഗര്‍ഭം ഇങ്ങിനെയല്ല. അതുകൊണ്ടു തന്നെ ഈ കഥയിലെ കഥാപാത്രങ്ങളും കഥാപരിസരവും സാങ്കല്‍പ്പികം മാത്രമാണു, മറിച്ച് തോന്നുന്നെങ്കില്‍ അതു കയ്യിലിരിപ്പിന്‍റെ ഗുണം

Crystal reports load report failed: Could not load file or assembly CrystalDecisions.Web, Version=10.2.3600.0

Recently we upgraded our Web Application Server to Windows Server 2008. After this, crystal reports in the VS2005 applications failed to load. This is because VS 2005 applications were using crystal report version 10.2.3600.0 which we can't install in Windows Server 2008. In the new server we can only install the crystal report version 10.5.3700.0. I had to follow the below steps to resolve this issue. Downloaded and installed new version of crystal report from the page below. http://wiki.sdn.sap.com/wiki/pages/viewpage.action?pageId=56787567 Changed versions(10.2.3600.0 to 10.5.3700.0) in Web.Config and report viewer pages We removed all the crystal report reference from the project and tried to refer the new versions. But new versions were not displayed in the list. So I decided to take a local copy of these dlls and refer them. To do this run the below command( as Admin ) regsvr32 -u C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\shfusion.dll The above command will help you

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

I was getting timeout error while executing a stored procedure from the web application. But this stored procedure is executing within a second while running from SQL management studio. This issue can happen when database's statistics and/query plan cache are incorrect. This can be resolved by updating statistics by executing  exec sp_updatestats Error: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding. Solution:  exec sp_updatestats If issue didn't resolve even after executing above this, you may need to optimize the query.