ASP.Net is a stateless connection. The pages gets recreated every time the page is posted back to the server. In order to maintain the changes done by the user, asp.net has different state management techniques.
ASP.Net supports Client-side and Server-side State Management options as listed below. In this article we will see client-side management techniques.
Client-side State Management
In this approach, the state information is stored on the client's computer/browser in the form of a URL, Cookies or as Hidden Fields in the webpage. Client side approach is scalable as it stores data on multiple servers i.e. each users information on their own system. There is no need for the server to remember their individual preferences. But on the other hand, client-side approach can create performance bottle necks if large data is stored, as the data would travel between client and server during postbacks.
Lets see these techniques in details with examples.
a. QueryString: With this option, you can pass values from one page to another as query strings, appended to the end of the page's url. Use this option if you are not passing any important values like email id, or any other parameter value as it is visible to the user.
Building a QueryString
The values in the query string passed as key/value pairs seperated by an '&' (ampersand). You can use Server.Transfer or Response.Write method to redirect to the other page by concatenating the parameters as below.
http://www.test.com/test.aspx?day=14&month=07&year=2010
Getting values from the querystring
Below example shows how to get the day, month & year values from the querystring. You can get the querystring value by passing in the name of the querystring or by index to the Request.QueryString object.
Request.QueryString["day"] or Request.QueryString[0]
Request.QueryString["month"] or Request.QueryString[1]
Request.QueryString["year"] or Request.QueryString[2]
b. Cookies: Cookies store the information on the users system. Information like login information, user preferences are generally stored in cookies. Cookies can be accessed across multiple pages. This option is not used to store large values as the information travels back and forth between client & server.
Store the cookies values in Form1.aspx and redirect to Form2.aspx
Response.Cookies["firstname"].Value = "John";
Response.Cookies["lastname"].Value = "Deller";
Server.Transfer("~/form2.aspx");
Retrive cookies value in Form2.aspx page using a label control
lblFinal.Text = Request.Cookies["firstname"].Value + " " + Request.Cookies["lastname"].Value;
c. Hidden Fields: Hidden Fields are stored at the client's side, as HTML on the web page but they are not visible to the user in the page's output on the browser.
It can be viewed by the user by opening the page source hence not used to store important data. As hidden fields also travels back and forth on each requests, it is recommended not to store large data in the hidden fields to avoid performance issues. It can be used to store data related to the user & their preferences.
Store values in hidden field
Insert a placeholder tag which will be replaced by the one that you pass using the code as below:
Aspx page:
<asp:PlaceHolder ID="plHolder" runat="server"></asp:PlaceHolder>
Code:
HtmlInputHidden _htmlInputHidden = new HtmlInputHidden();
_htmlInputHidden.ID = "promotion";
_htmlInputHidden.Value = "50% discount on men's wear";
plHolder.Controls.Add(_htmlInputHidden);
Retrieving the value from hidden field
String strHiddenValue = ((HtmlInputHidden)plHolder.FindControl("promotion")).Value;
d. View State: View State are also stored as HTML on the web page, similar to hidden fields. The data stored in the view state is specific to that page, it cannot be passed to the other pages.
Data can be retained in the view state in between post backs. Most of the server controls in asp.net use view state to retain the data entered by the user during post backs. Custom values can be stored in the view state object.
Again, as this is passed between client and server during post backs, store small amount of data to avoid performance bottlenecks.
Storing value in view state: ViewState["test"] = "This is a test string";
Retrieving value from view state: String strViewState = ViewState["test"];
e. Control State: Control state is like View State but reserved for the custom control you develop. So if you are building your own custom control, its better to use Control State so the users using your control do not break your control by disabling View State.
Refer: http://www.pluralsight.com/community/blogs/fritz/archive/2004/07/01/472.aspx for more details & example.
References
http://msdn.microsoft.com/en-us/library/75x4ha6s.aspx
Good Concept.
Keep it up.