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.

544 x CHAPTER 14 LINQ AND THE ADO.<strong>NET</strong> ENTITY FRAMEWORK<br />

access<strong>in</strong>g your site over a slow network connection. Instead of present<strong>in</strong>g them all the images <strong>in</strong> the<br />

photo album on a s<strong>in</strong>gle page, you can split up the photo album <strong>in</strong>to multiple pages, enabl<strong>in</strong>g users<br />

to go from page to page. You see how to do this <strong>in</strong> the next section, which discusses the DataPager<br />

control.<br />

Introduc<strong>in</strong>g the DataPager Control<br />

The DataPager is a separate control that you can use to extend another, data-bound, control.<br />

Currently, the .<strong>NET</strong> Framework lets you use the DataPager only to provide pag<strong>in</strong>g capabilities to<br />

the ListView control, but the developer community has been active writ<strong>in</strong>g implementations for<br />

other controls, like the GridView, as well.<br />

You can hook up the DataPager to the ListView control <strong>in</strong> two ways: You can either def<strong>in</strong>e it<br />

with<strong>in</strong> the of the ListView control or you can def<strong>in</strong>e it entirely outside the<br />

ListView. In the first case, the DataPager knows to what control it should provide pag<strong>in</strong>g capabilities<br />

automatically. In the latter case, you need to set the PagedControlID property of the DataPager<br />

to the ID of a valid ListView control. You see how to configure <strong>and</strong> use the DataPager <strong>in</strong> conjunction<br />

with a ListView next. Be<strong>in</strong>g able to def<strong>in</strong>e the DataPager outside of the ListView control is<br />

useful if you want to place it at a different location of the page, such as <strong>in</strong> the Footer or Sidebar<br />

area, for example.<br />

TRY IT OUT<br />

Pag<strong>in</strong>g Data with the ListView <strong>and</strong> DataPager Controls<br />

In this Try It Out you create the front-end page of the Gig Pics feature. Users of your site can choose<br />

one of the available photo albums from a drop-down list <strong>and</strong> then view all the available pictures <strong>in</strong> a<br />

pageable list that is created by a ListView <strong>and</strong> a DataPager control. Figure 14-22 shows the f<strong>in</strong>al result<br />

of this exercise.<br />

1. In the root of your site, create a new folder called PhotoAlbums. Inside this folder create a new<br />

Web Form based on your custom page template <strong>and</strong> call it Default.aspx. Set the Title of the<br />

page to All Photo Albums.<br />

2. Switch to Design View <strong>and</strong> drop a DropDownList control on the page. On the control’s Smart<br />

Tasks panel, enable AutoPostBack <strong>and</strong> then hook it up to a new EntityDataSource control by<br />

click<strong>in</strong>g Choose Data Source, <strong>and</strong> then choos<strong>in</strong>g from the first drop-down list.<br />

Click the Entity icon, click OK, <strong>and</strong> select PlanetWroxEntities <strong>in</strong> the Named Connection dropdown<br />

list. Click Next. On the Configure Data Selection dialog box, choose PhotoAlbums from the<br />

EntitySetName drop-down list <strong>and</strong> select the fields Id <strong>and</strong> Name, as shown <strong>in</strong> Figure 14-20.<br />

3. Click F<strong>in</strong>ish to close the Configure Data Source wizard. In the Choose a Data Source dialog box,<br />

select Name from the Data Field to Display drop-down list <strong>and</strong> leave Id selected <strong>in</strong> the Data Field<br />

for the Value drop-down list. If you don’t see the items <strong>in</strong> the lists, click Refresh Schema first.<br />

4. Click OK to close the Data Source Configuration Wizard.<br />

5. Below the DropDownList add a new ListView control <strong>and</strong> connect it to a new EntityDataSource<br />

by select<strong>in</strong>g from the drop-down list on the control’s Smart Tasks panel. Click<br />

the Entity icon <strong>and</strong> click OK. Select PlanetWroxEntities <strong>in</strong> the drop-down <strong>and</strong> click Next. In the<br />

dialog box that follows, choose Pictures from the EntitySetName drop-down list <strong>and</strong> click F<strong>in</strong>ish.

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

Saved successfully!

Ooh no, something went wrong!