This is necessary because otherwise the new theme won’t be applied immediately. Because the theme
needs to be set early in the page’s life cycle, it can no longer be set for the current request. By redirecting
the user to the same page, a new request is made that can successfully apply the selected theme. The
next exercise shows you the code to set the selected theme programmatically.
The fi nal change in the master page you made was a modifi cation to the Page_Load handler. Inside this
method, a String variable is declared that holds the currently active theme by looking at Page.Theme.
This serves as the default theme and will be the one that is preselected in the drop-down list if the
user doesn’t have a cookie holding her preferred theme. The code then sees if there is a cookie called
PreferredTheme. If it exists, its value is used to give the string selectedTheme a new value. In the end,
this String variable is then used to fi nd the item in the drop-down list and preselect it.
This way, the drop-down list always displays the currently confi gured site theme or the item the user
has chosen manually, even if she comes back to the site next week. Note the use of the FindByValue
method on the Items collection of the DropDownList control. This method returns the item if it is
found or Nothing (null in C#) when the item isn’t there. This ensures that if the cookie contains a
theme that is no longer available, the code doesn’t try to preselect an item in the list that doesn’t exist.