03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

242 x CHAPTER 6 CREATING CONSISTENT LOOKING WEBSITES<br />

6. Save all changes <strong>and</strong> then request Default.aspx <strong>in</strong> your browser aga<strong>in</strong>. The drop-down list <strong>in</strong> the<br />

sidebar should display the first item <strong>in</strong> the list as selected. Select the other option from the list <strong>and</strong><br />

the page will reload. The item you chose last <strong>in</strong> the drop-down list should now be preselected<br />

<strong>in</strong> the drop-down list. Close your browser <strong>and</strong> then browse to Default.aspx aga<strong>in</strong>. The theme<br />

you chose should still be selected <strong>in</strong> the drop-down list. Notice that you keep see<strong>in</strong>g the same<br />

theme because you haven’t written any code yet that applies the selected theme. You see how to do<br />

that <strong>in</strong> a later exercise.<br />

COMMON MISTAKES If you get an error, make sure you have no typos <strong>in</strong> the<br />

code. If noth<strong>in</strong>g seems to happen (for example, the page doesn’t post back),<br />

check if you set the AutoPostBack attribute on the DropDownList control to<br />

True. Also, check the spell<strong>in</strong>g of the name of the cookie (PreferredTheme) <strong>in</strong><br />

both code blocks. F<strong>in</strong>ally, if the correct item is not preselected after a postback<br />

or after you close <strong>and</strong> reopen your browser, check you browser’s or computer’s<br />

security sett<strong>in</strong>gs. You may have confi gured your browser to delete cookies<br />

when you close the browser, or you may have security software runn<strong>in</strong>g on your<br />

mach<strong>in</strong>e that blocks cookies altogether. If you can’t make it work <strong>in</strong> one browser,<br />

try it <strong>in</strong> another to rule out problems with the code.<br />

How It Works<br />

You made three important changes to the master page. First, you added the drop-down list <strong>and</strong> set<br />

AutoPostBack to True. This causes the page to submit itself back to the server as soon as you choose<br />

a new item <strong>in</strong> the list. When that happens, the code <strong>in</strong> the SelectedIndexChanged h<strong>and</strong>ler fires. This<br />

code creates a cookie that can be stored on the user’s computer. To make the cookie last between<br />

browser sessions, you need to set the Expires property. In the code example, the cookie is set to expire<br />

three months from now, which means the browser will discard it automatically after that period. Every<br />

time the user chooses a new theme, this date is extended, sett<strong>in</strong>g it for another three months:<br />

<strong>VB</strong>.<strong>NET</strong><br />

Dim preferredTheme As HttpCookie = New HttpCookie("PreferredTheme")<br />

preferredTheme.Expires = DateTime.Now.AddMonths(3)<br />

C#<br />

HttpCookie preferredTheme = new HttpCookie("PreferredTheme");<br />

preferredTheme.Expires = DateTime.Now.AddMonths(3);<br />

After the cookie has been created, you can set its Value property. In the example, the SelectedValue<br />

of the DropDownList (conta<strong>in</strong><strong>in</strong>g the name of the theme) is stored <strong>in</strong> the cookie. The cookie is then<br />

added to the Cookies collection us<strong>in</strong>g Response.Cookies.Add:<br />

<strong>VB</strong>.<strong>NET</strong><br />

preferredTheme.Value = ThemeList.SelectedValue<br />

Response.Cookies.Add(preferredTheme)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!