Saving the Preferences


After your user has set preferences, you ll want to save them someplace so that you can apply them when needed. When users have set their preferences, they click the Save Preferences button to store their settings. You re going to save the user s preferences in a cookie, so the Click handler for the Save Preferences button needs to read all the current controls (again) and write out a cookie.

About Cookies

A cookie is a tiny text file that your program sends to the browser along with the page. The browser saves the cookie in a special directory on the user s computer. When the user requests the page again, the browser sends the cookie back up to the server along with the request. Your program can then read the cookie and retrieve the information that you stored earlier. The cookie is like a little invisible attachment, sent back and forth between browser and server along with the page. More precisely, a cookie is attached to all pages in the same application. For our purposes, an application consists of all the pages that are in the same folder or in a subfolder. That s why I told you to create the preferences page in the same directory as the slideshow page so that the preference and slideshow pages would be in the same application and therefore share the same cookie. If you were to save preferences in a cookie and then send the user to a page in some other application, the new page wouldn t be able to read the cookie.

When you store information in a cookie, you give the cookie a name. You can then store individual values by name in the cookie. Cookies are stored as strings in a text file. This format introduces a slight problem for the user s color choice. I mentioned earlier in this chapter that ASP.NET has an internal format for colors. Before you store a color in a cookie, therefore, you have to convert the color to a string. Later, after you ve read the color name out of the cookie, you ll need to convert the name from a string back into a Color object. I ll show you a technique for the conversion in the next section.

Finally, a cookie lives only as long as you specify. By default, the cookie is stored only for the duration of the user s session when users close their browsers, the cookie disappears. That s not practical for scenarios like ours in which you want the user s settings to be persistent. To maintain persistent information, you need to specify the cookie s expiration date. The rule of thumb is to choose the shortest practical lifetime. Will your users need their preferences 10 years from now? One year? One month? You decide. Whatever lifetime you set, remember that anything you store in a cookie can be lost if users clean all the cookies off their hard disk, as users occasionally do for housekeeping purposes.

Cookies sometimes get a bad rap. Many people distrust cookies for a number of reasons. They are suspicious when information is written to their computers without their knowledge. (By default, the browser doesn t tell you that it s saving cookies on your hard disk.) They are also concerned about privacy and about being tracked by Web sites. These concerns are all legitimate. In the next section, I m going to show you what I think is a legitimate use of cookies. If users have set their browsers to refuse cookies, the technique I illustrate in the following section won t work for them.

Programming the Save Preferences Button

After setting your preferences, you save them in a cookie by clicking the Save Preferences button. To create a handler for the Save Preferences button, double-click the button in Design view and add the following code:

Sub buttonSavePreferences_Click(sender As Obje ct, e As EventArgs    showCaption = checkShowCaptions.Checke    captionColorName =          labelSampleCaption.ForeColor.ToKnownColor.ToStr ing(    captionFontName = labelSampleCaption.Font.Nam    Response.Cookies("Preferences")("showCaption") = s howCaptio    Response.Cookies("Preferences")("captionColor") = c aptionColorNam    Response.Cookies("Preferences")("captionFontName" ) = captionFontNam    Response.Cookies("Preferences").Expires = Now.Add Months(2 End Sub

This code gets the control values, adds them to a cookie, and sets the cookie s expiration date to two months from now.

You control cookies using the Response object. Remember that a cookie is sent to the browser along with the page. The Response object gives you a mechanism for manipulating the stream of data that constitutes the page and any additional information that goes with it to the browser. In this case, as you can see, the Response object has a property named Cookies that lets you set a cookie. When you create a cookie, you specify two values for the Response object s Cookies collection: a cookie name and the variable to set. The cookie name identifies which cookie in the collection you want to set. Different Web pages can store information under different names in the Cookies collection, so you must always specify what cookie to use. Each individual cookie item in the Cookies collection can in turn store multiple values in different variables. To store a value in a cookie, then, you need to identify what variable within that cookie to store the value in.

To set the expiration date, you set the Expires property of an individual cookie. (Different cookies in the Cookies collection can have different expiration dates.) You can set the expiration to any interval from seconds to years. In the example, I started with the value Now, which is a keyword in Visual Basic .NET that returns a DateTime value representing the current moment. Because I want to set the expiration to two months from now, I call the AddMonths method, which is available for any DateTime value. (The DateTime class also supports an AddSeconds method, an AddMinutes method, and other time-incrementing methods.) The 2 in the AddMonths method call, obviously, adds two months to the expiration date.

Other than the cookie itself, the only unusual thing here is the color conversion I mentioned in the previous section. You need to convert a Color object into a string before storing it in the cookie, as shown in this line:

captionColorName = labelSampleCaption.ForeColo r.ToKnownColor.ToString()

The line first retrieves the current color of the sample caption via the ForeColor property. It then calls the ToKnownColor method to convert the Color object into a number indicating where in the list of 167 known colors that color lies. Finally, the ToString method tells the Color object to look up that number in the table and return the known color name for that number. Then it assigns the color name to the variable captionColorName. This line of code isn t at all intuitive. If you re having a hard time understanding the color conversion process, you can simply think of the whole process as a small chunk of code that converts colors into strings.




Microsoft ASP. NET Web Matrix Starter Kit
Microsoft ASP.NET Web Matrix Starter Kit (Bpg-Other)
ISBN: 0735618569
EAN: 2147483647
Year: 2003
Pages: 169
Authors: Mike Pope
BUY ON AMAZON

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net