HeaderText='pub_id'

 SortExpression='pub_id'/>

with this:

<asp:TemplateField

 HeaderText='Publisher'>

 <ItemTemplate>

  <%#Eval('publisher.pub_name')%>

 </ItemTemplate>

</asp:TemplateField>

Essentially, this changes the header for the publisher column in the GridView to Publisher, and the values are now derived from the publisher.pub_name property of the DataClassesDataContext class.

 Press F5 to debug the application again to see the publishers' names instead of the publishers' IDs (see Figure 17-16).

Figure 17-16 

Displaying Titles from a Selected Publisher

So far, all the titles in the titles table are displayed in the GridView control. You might want to restrict the titles displayed to a particular selected publisher. To do so, insert another LinqDataSource control to the Default.aspx page by adding the following highlighted code:

<asp:LinqDataSource

 ID='LinqDataSource1'

 runat='server'

 ContextTypeName='DataClassesDataContext'

 EnableDelete='True'

 EnableInsert='True'

 EnableUpdate='True'

 TableName='titles'>

</asp:LinqDataSource>

<asp:LinqDataSource

 ID='LinqDataSource2'

 runat='server'

 ContextTypeName='DataClassesDataContext'

 OrderBy='pub_name'

 Select='new(pub_name, pub_id)'

 TableName='publishers'>

</asp:LinqDataSource>

Notice that the second LinqDataSource control has the Select attribute where you can specify the name of the fields you want to retrieve (pub_name and pub_id, in this example).

Add a DropDownList control to the top of the page by adding the following highlighted code:

<body>

 <form id='form1' runat='server'>

  <div>

   Display titles by publisher:

   <asp:DropDownList

    ID='DropDownList1'

    runat='server'

    DataSourceID='LinqDataSource2'

    DataTextField='pub_name'

    DataValueField='pub_id'

    AutoPostBack='True'>

   </asp:DropDownList>

   <asp:GridView ID='GridView1' runat='server'

    ...

    ...

This addition binds a DropDownList control to the LinqDataSource control. The DropDownList control will display the list of publisher names (pub_name), and each publisher's name has the pub-id as its value.

Default.aspx should now look like Figure 17-17 in design view. You will see the text 'Display titles by publisher:' as well as a dropdown list control.

Figure 17-17

To configure the first LinqDataSource control so that the GridView control will only display titles from the selected publisher, click on the SmartTag of the GridView control, and click the Configure Data Source link (see Figure 17-18).

Figure 17-18

Click Next, and then click the Where button. Enter the following values in the dialog (see Figure 17- 19).

Condition Value
Column pub_id
Operator ==
Source Control
Control ID DropDownList1
Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату