03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Underst<strong>and</strong><strong>in</strong>g Profile x 665<br />

sent to the database to fetch the requested genres <strong>and</strong> reviews. This means that filter<strong>in</strong>g of the requested<br />

genres takes place at the database level, <strong>and</strong> not <strong>in</strong> the <strong>ASP</strong>X page. This <strong>in</strong> turn means that fewer rows<br />

are transferred from the database to the <strong>ASP</strong>X page (only those that are really needed), which results <strong>in</strong><br />

better performance.<br />

The profile property FavoriteGenres returns an empty list, rather than throw<strong>in</strong>g an exception for<br />

anonymous users. So, even users with no profile can safely view this page. Instead of see<strong>in</strong>g any<br />

reviews, they get a message stat<strong>in</strong>g they haven’t set their genre preferences yet, or that they need to log<br />

<strong>in</strong> first.<br />

In the end of the Page_Load h<strong>and</strong>ler, some code determ<strong>in</strong>es whether to show or hide the Repeater <strong>and</strong><br />

the NoRecords controls:<br />

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

GenreRepeater.Visible = GenreRepeater.Items.Count > 0<br />

NoRecords.Visible = Not GenreRepeater.Visible<br />

C#<br />

GenreRepeater.Visible = GenreRepeater.Items.Count > 0;<br />

NoRecords.Visible = !GenreRepeater.Visible;<br />

If after data b<strong>in</strong>d<strong>in</strong>g the outer Repeater, its Items collection is still empty, it means no genres were<br />

found for the current user. If that’s the case, the entire Repeater is hidden <strong>and</strong> the PlaceHolder is<br />

shown. However, if the Count property of the Items collection is larger than zero, the Repeater is<br />

made visible <strong>and</strong> the PlaceHolder is hidden.<br />

In Chapter 14, you created a page called NewPhotoAlbum.aspx that lets users <strong>in</strong>sert new Gig Pics<br />

albums. The current implementation of this page has a few shortcom<strong>in</strong>gs. First of all, anyone can<br />

<strong>in</strong>sert a new album. There’s no way to block anonymous users from creat<strong>in</strong>g a new album <strong>and</strong><br />

upload<strong>in</strong>g pictures.<br />

Secondly, only Managers can remove pictures from an exist<strong>in</strong>g photo album. It would be nice if the<br />

owner of an album could also remove her own pictures. Now that you know more about security<br />

<strong>and</strong> personaliz<strong>in</strong>g web pages, this is pretty easy to implement, as you see <strong>in</strong> the follow<strong>in</strong>g exercise.<br />

TRY IT OUT<br />

Lett<strong>in</strong>g Users Manage Their Own Photo Albums<br />

In this Try It Out you see how to block the NewPhotoAlbum.aspx <strong>and</strong> ManagePhotoAlbum.aspx pages<br />

from unauthenticated users. Additionally, you see how to record the name of the user who created the<br />

photo album <strong>and</strong> use that name later on to enable users to alter their own photo albums.<br />

1. Open SQL Server Management Studio from the W<strong>in</strong>dows Start menu or Start screen. Open your<br />

PlanetWrox database, <strong>and</strong> locate the PhotoAlbum table. Right-click it <strong>and</strong> choose Design. Add<br />

a new column called UserName, set its data type to nvarchar(256), <strong>and</strong> leave the Allow Nulls<br />

option selected. (This table already conta<strong>in</strong>s photo albums without a valid UserName, so you can’t<br />

make the column required at this stage unless you delete these photo albums <strong>and</strong> their related

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

Saved successfully!

Ooh no, something went wrong!