|
The other half of setting preferences is reading them. Rather than starting from scratch, let s just reuse the slideshow page you created in Chapter 5 and modify it to read preferences. You re not going to change the basic functionality of the page. You just need to add code to get the cookie (if any), read its values, and set the slide caption properties appropriately.
To create the new slideshow page, in Windows Explorer, make a copy of Slideshow1.aspx and name it Slideshow2.aspx. The Slideshow2.aspx file should be in the same directory as the SlideshowSettings.aspx file.
The logical time to read the cookie and configure the caption is when the page first loads, before you ve shown anything on the page. You already have a Page_Load handler in the slideshow page, where you initialize the arrays for the slides and captions and where you restore the current slide number from viewstate. You just need to add more logic to the handler for this new scenario.
Modify the slideshow to read the cookie and set preferences
Open Slideshow2.aspx in Web Matrix, and switch to Code view.
In the Page_Load handler, add the boldfaced code in the following listing:
Sub Page_Load( Path = "images\ slideNames(0) = "Mexico_church1.jpg slideNames(1) = "Mexico_church2.jpg ⋮ If IsPostBack = True The currentSlide = Viewstate("currentSlid e" Els currentSlide = Viewstate("currentSlide") = currentSli d imageSlide.imageUrl = path & slidename s(currentSlide labelNavbar.Text = "Slide " & currentSlide+1 & " of " & slideNames.Lengt labelCaption.Text = captions(currentSl ide End I   Dim showCaptions As Boolea Dim captionFont As Strin Dim captionColorName As Strin If Request.Cookies("Preferences") Is Nothing The showCaptions = Tru captionFont = "Arial, Helvetica captionColorName = "Black Els showCaptions = Request.Cookies("Preferences") ("showCaption" captionFont = Request.Cookies("Preferences") ("captionFontName" captionColorName = Request.Cookies("Preferences ") ("captionColor" End I If showCaptions = True The labelCaption.Visible = Tru labelCaption.Font.Name = captionFon labelCaption.ForeColor = System.Drawing.Color.FromName(captionColorN ame Els labelCaption.Visible = Fals End I End Sub
You read a cookie in much the same way you wrote it, except that you work with the Request object instead of the Response object. The Request object gives you programmatic access to the information that the browser is sending to ASP.NET.
Notice the following test in the code:
If Request.Cookies("Preferences") Is Nothing T hen
This line tests whether there is a cookie at all. The syntax Is Nothing in Visual Basic is the test to see whether an object exists. In our scenario, we re imagining that users will start at the preferences page, set the font and colors, and then display the slideshow page. In that case, the cookie will be there. However, there s no guarantee that users won t go directly to the slideshow page. In other words, users might be able to get to the slideshow page without first setting a cookie. If there s no cookie, a line like the following one will throw a rather nasty error:
showCaptions = Request.Cookies("Preferences")( "showCaption")
Therefore, the program tests for a cookie first, and if there is no cookie, it sets default values for the various caption settings. If the cookie is found, we can proceed with setting the caption properties.
Based on the value of showCaptions, the program sets the Visible property of labelCaption. As you would expect, if Visible is set to false, no caption is shown. In fact, the caption isn t rendered to the browser at all, which can have side effects on the page layout. To avoid the side effects, don t use the Visible property. Instead, set the Text property of the browser to " " (one space), which will render the caption without any text that is, invisibly.
|