oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Displaying Master-Details Relationships in ASP.NET 2.0
Pages: 1, 2, 3, 4, 5

Basically this means that the value for the au_id column in GridView2 is obtained from the GridView1 control. GridView1, if you recall, has the DataKeyNames attribute set to "au_id":

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
AllowSorting="True" AutoGenerateColumns="False"  
DataKeyNames="au_id" DataSourceID="SqlDataSource1">

Hence, the au_id of the author selected in GridView1 will be used as the value for the au_id column in GridView2.

The following shows the source view of what we have just added in this section:

<asp:GridView ID="GridView2" runat="server" 
   AutoGenerateColumns="False" DataSourceID="SqlDataSource2">
      <asp:BoundField DataField="title_id" HeaderText="title_id" 
         SortExpression="title_id" />

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
   ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
   SelectCommand="SELECT [title_id] FROM [titleauthor] WHERE ([au_id] = @au_id)">
      <asp:ControlParameter ControlID="GridView1" Name="au_id" 
         Type="String" />

As usual, check the various checkboxes in the GridView's Smart tag to enable it for paging, sorting, and selection (see Figure 11). Also beautify it by applying the Autumn scheme.

Figure 11. Configuring the second GridView control

Press F5 to debug the application. You will see that when you now select an author from the first GridView control, the corresponding title IDs of the books written by the selected author will be displayed in the second GridView control (see Figure 12).

Figure 12. Displaying the title ids of books authored by the selected author

The final step is to add a DetailsView control to the page so that you can view the details of the book written by the author.

Drag and drop the DetailsView control onto default.aspx and configure a new data source (see Figure 13).

Figure 13. Adding the DetailsView control to the page

For the table name, choose titles and select all columns (see Figure 14). Click on the WHERE… button.

Figure 14. Configuring the data source for the DetailsView control

Pages: 1, 2, 3, 4, 5

Next Pagearrow