Creating the Preferences Page


The page you ll create incorporates all the basic features of getting and setting user preferences: presenting choices to the user, storing the preferences somewhere, and applying them as appropriate.

There are many ways in which you could ask the user for preferences. In this example, you ll create two versions of the preferences page, a page that illustrates the basic principles of storing preferences, and a smarter page that automatically previews your preference choices. Both versions have the following basic functionalities:

  • A check box that lets the user choose whether to show captions.

  • A drop-down list that contains a selection of colors to let the user set the caption color.

  • A set of option buttons that lets the user select between three fonts: Arial, Times Roman, and Courier.

You ll also add a preview feature that shows a sample caption in the color and font that the user has selected. If the user chooses not to show captions, the sample caption will disappear. After setting all the preferences, the user clicks a Save button. The Web page gathers the user s preferences and stores them in a cookie. (I ll discuss cookies in detail later in this chapter.) Finally, the user will be able to click a button to jump to the slideshow.

Later in the chapter, you ll add an instant preview feature to the preferences page so that the sample caption will reflect changes as soon as the user makes them.

We ll start by creating a new page where users can set preferences. To get you oriented, Figure 6-1 shows what the preferences page might look like when finished.

click to expand
Figure 6-1: The preferences page in action.

Create the preferences page and add controls

  1. In the directory where you created  Slideshow1.aspx, create a new page named  SlideshowSettings.aspx.

  2. Add a heading and captions as static HTML. Follow the model of the preceding screen shot for the layout.

  3. From the Web Controls tab of the Toolbox, add the following controls to the page and set the specified properties. (As always, the settings I suggest are not gospel; you can name the controls as you like. Just be sure to substitute your names as appropriate later in the code.)

    Control

    Property Settings

    CheckBox

    ID: checkShowCaptions

    Checked: True

    Text: Show captions

    DropDownList

    ID: listColorNames

    RadioButton

    ID: radioFontArial

    GroupName: fontName (Option buttons that have the same group name act as a unit, allowing only one to be selected at a time.)

    Checked: True (This button will be checked by default.)

    Text: Arial

    RadioButton

    ID: radioFontTimes

    GroupName: fontName (Same as the previous radio button)

    Text: Times Roman

    RadioButton

    ID: radioFontCourier

    GroupName: fontName (Same as the two previous option buttons)

    Text: Courier

    Label

    ID: labelSampleCaption

    Text: Sample Caption

    Font.Name: Arial

    Button

    ID: buttonPreview

    Text: Preview

    Button

    ID: buttonSavePreferences

    Text: Save Preferences

    Button

    ID: buttonDisplaySlideshow

    Text: Display slideshow

Populating the DropDownList Control

The drop-down list needs to show a list of colors. In this page, you ll hard- code the colors into the DropDownList control. It s also possible to load the list dynamically in various ways, as I ll explain in Chapter 12.

Add items to the DropDownList control

  1. In Design view, select the DropDownList control.

  2. In the Properties window, click inside the Items property box. An ellipsis button will be visible. Click that button to display the ListItem Collection Editor dialog box, as shown in this screen shot.

    click to expand

The items in a DropDownList control (and other controls, too, such as ListBox) are stored in a collection. A collection is like the array you used in Chapter 5 to store the file names and captions of the photos. You add elements differently than in an array, though, and you can reference the elements not just by index number, but also by a name. For now, just make a note that the items in a DropDownList are members of a collection.

  • In the dialog box, click Add. Under Members, you will see a new item called ListItem. In the right-hand pane is a mini Properties window specifically for that item.

  • Set the Text property of ListItem to Black. Notice that the name of the item changes to Black in the left-hand pane as well, as shown here:

    click to expand

  • Add several more items and change the text for each one. I suggest that you name them Red, Green, DarkGreen (one word), Pink, and Blue, as shown in the following illustration. However, you can add as many colors as you like. (See the sidebar at the end of this procedure for color choices.)

    click to expand

  • Set the value of the ForeColor property for the labelSampleCaption control to Black, the value of the first item in the drop-down list. That way, the caption will be preset to the default value in the drop-down list. Your controls should be all set now.

  • Press F5 and run the page.

    You should be able to view a list of colors and check and uncheck the check box. Make sure that when you click the option buttons, they are mutually exclusive. (If not, check that all the option buttons have their GroupName property set to the same name.)

  • Obviously, changing the settings has no effect, because you haven t actually written the code to do anything with the settings. Close the browser, and let s get to work on the programming.

    start sidebar
    Color Choices

    You might wonder what colors you can choose. Microsoft ASP.NET supports a list of 167 so-called known colors that you can specify by name. If you know the HTML color names supported by Internet Explorer, those are the colors supported by ASP.NET. The list includes all the basic color names, plus Cyan, Magenta, and other standard colors. Some colors have the light qualifier (lightgreen) or the dark qualifier (darkred). The list also includes some fanciful names that are variants on the basic colors, such as BurlyWood (tan), Gainsboro (gray), and Tomato (orange). To see a complete list of color names, select a control on the page, click the button in the ForeColor box in the Properties window, and select the Web tab.

    end sidebar



    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