Книга: C# 2008 Programmer

Displaying Titles from a Selected Publisher

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 runat="server">
  <div>
   Display titles by publisher:
   <asp:DropDownList
   
    runat="server"
    DataSourceID="LinqDataSource2"
    DataTextField="pub_name"
    DataValueField="pub_id"
    AutoPostBack="True">
   </asp:DropDownList>
   <asp:GridView 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

Figure 17-19

Click Add, OK, and then Finish. Visual Studio 2008 will ask if you want to regenerate the GridView columns fields and data keys. Click No.

This will make the GridView control display titles whose pub_id file match the pub-id value of the selected publisher in the DropDownList1 control.

The source of the LinqDataSource control now looks like this:

<asp:LinqDataSource
 ID="LinqDataSource1"
 runat="server"
 ContextTypeName="DataClassesDataContext"
 EnableDelete="True"
 EnableInsert="True"
 EnableUpdate="True"
 TableName="titles"
 Where="pub_id == @pub_id">
 <WhereParameters>
  <asp:ControlParameter
   ControlID="DropDownList1"
   Name="pub_id"
   PropertyName="SelectedValue"
   Type="String"/>
 </WhereParameters>
</asp:LinqDataSource>

Press F5 to debug the application. When you select a publisher now, all books published by that publisher are displayed in the GridView control (see Figure 17-20).


Figure 17-20

Оглавление книги


Генерация: 1.160. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз