15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

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

1178 ❘ ChaPTer 40 cOre Asp.net<br />

(continued)<br />

TemPlaTe aPPlies To desCriPTion<br />

<br />

<br />

The easiest way to underst<strong>and</strong> how to use these is through an example.<br />

Using Templates<br />

GridView, DetailsView,<br />

FormView, ListView<br />

GridView, DetailsView,<br />

FormView<br />

You will extend the table at the top of the Default.aspx page of PCSWebSite3 to contain a ListView<br />

displaying each of the events stored in the database. You will make these events selectable such that details<br />

of any event can be displayed by clicking on its name, in a FormView control.<br />

First, you need to create new data sources for the data-bound controls. It is good practice (<strong>and</strong> strongly<br />

recommended) to have a separate data source for each data-bound control.<br />

The SqlDataSource control required for the ListView control, MRBEventData2, is similar to<br />

MRBEventData, except that it needs to return only Name <strong>and</strong> ID data. The required code is as follows:<br />

<br />

<br />

The data source for the FormView control, MRBEventDetailData, is more complicated, although you can<br />

build it easily enough through the data source Configuration Wizard. This data source uses the selected item<br />

of the ListView control, which you will call EventList, to get only the selected item data. This is achieved<br />

using a parameter in the SQL query, as follows:<br />

<br />

<br />

<br />

<br />

<br />

Used to display empty items — for<br />

example, when no records are<br />

available in a GridView<br />

Used to format pagination<br />

ListView Used to specify the output surrounding<br />

groups of items<br />

ListView Used between groups of items<br />

ListView When using item groups, used to<br />

supply output for empty items in a<br />

group. This template is used when<br />

there are not enough items in a<br />

group to fill the group.<br />

Here, the ID parameter results in a value being inserted in place of @ID in the select query. The<br />

ControlParameter entry takes this value from the SelectedValue property of EventList, or uses –1 if<br />

there is no selected item. At first glance, this syntax seems a little odd, but it is very flexible, <strong>and</strong> after you’ve<br />

generated a few of these using the wizard, you won’t have any trouble assembling your own.<br />

Next, you need to add the ListView <strong>and</strong> FormView controls. The changes to the code in Default.aspx in<br />

the PCSWebSite3 project are shown in the following code:<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!