Using Client-Side Cookies
The following sample illustrates the use of client-side cookies to store volatile user preferences.
Storing cookies on the client is one of the methods that ASP.NET's session
state uses to associate requests with sessions. Cookies can also be used
directly to persist data between requests, but the data is then stored
on the client and sent to the server with every request. Browsers place
limits on the size of a cookie; therefore, only a maximum of 4096 bytes is guaranteed
to be acceptable.
When the data is stored on the client, the Page_Load method in the file
cookies1.aspx checks whether the client has sent a cookie. If not, a new
cookie is created and initialized and stored on the client:
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Request.Cookies("preferences1") = Null Then
Dim cookie As New HttpCookie("preferences1")
cookie.Values.Add("ForeColor", "black")
...
Response.Cookies.Add(cookie)
End If
End Sub
VB
On the same page, a GetStyle method is used again to provide
the individual values stored in the cookie:
Protected Function GetStyle(key As String) As String
Dim cookie As HttpCookie = Request.Cookies("preferences1")
If cookie <> Null Then
Select Case key
Case "ForeColor"
Return(cookie.Values("ForeColor"))
Case ...
End Select
End If
Return("")
End Function
VB
Verify that the sample works by opening the cookies1.aspx page and modifying
the preferences. Open the page in another window, it should pick up the new
preferences. Close all browser windows and open the cookies1.aspx page again.
This should delete the temporary cookie and restore the default preference values.
To make a cookie persistent between sessions, the Expires property on
the HttpCookie class has to be set to a date in the future. The following
code on the customization.aspx page is identical to the previous sample,
with the exception of the assignment to Cookie.Expires:
Protected Sub Submit_Click(sender As Object, e As EventArgs)
Dim cookie As New HttpCookie("preferences2")
cookie.Values.Add("ForeColor",ForeColor.Value)
...
cookie.Expires = DateTime.MaxValue ' Never Expires
Response.Cookies.Add(cookie)
Response.Redirect(State("Referer").ToString())
End Sub
VB
Verify that the sample is working by modifying a value, closing all browser
windows, and opening cookies2.aspx again. The window should still show the
customized value.
Using ViewState
This sample illustrates the use of the ViewState property to store request-specific values.
ASP.NET provides the server-side notion of a view state for each control. A control can save its
internal state between requests using the ViewState property on an instance of the class StateBag. The
StateBag class provides a dictionary-like interface to store objects associated with a string key.
The file pagestate1.aspx displays one visible panel and stores the index of it in the view
state of the page with the key PanelIndex:
Protected Sub Next_Click(sender As Object, e As EventArgs)
Dim PrevPanelId As String = "Panel" + ViewState("PanelIndex").ToString()
ViewState("PanelIndex") = CType(ViewState("PanelIndex") + 1, Integer)
Dim PanelId As String = "Panel" + ViewState("PanelIndex").ToString()
...
End Sub
VB
Note that if you open the page in several browser windows, each browser window will
initially show the name panel. Each window can independently navigate between the panels.
- Use application state variables to store data that is modified infrequently but used often.
- Use session state variables to store data that is specific to one session or user.
The data is stored entirely on the server. Use it for short-lived, bulky, or sensitive data.
- Store small amounts of volatile data in a nonpersistent cookie. The data is stored on the
client, sent to the server on each request, and expires when the client ends execution.
- Store small amounts of non-volatile data in a persistent cookie. The data is stored on the
client until it expires and is sent to the server on each request.
- Store small amounts of request-specific data in the view state. The data is sent from the server
to the client and back.
Copyright 2001-2002 Microsoft Corporation. All rights reserved.