oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

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

Configure the Add WHERE Clause dialog as shown in Figure 15 and click on Add. The title_id column in the DetailsView control will now be obtained from the title_id field from GridView2.

Figure 15. Setting the values for the WHERE clause

The following shows the source view of what you have just added:

<asp:DetailsView ID="DetailsView1" runat="server" 
   AutoGenerateRows="False" DataKeyNames="title_id"
   DataSourceID="SqlDataSource3" Height="50px" Width="125px">
      <asp:BoundField DataField="title_id" HeaderText="title_id" 
         ReadOnly="True" SortExpression="title_id" />
      <asp:BoundField DataField="title" HeaderText="title" 
         SortExpression="title" />
      <asp:BoundField DataField="type" HeaderText="type" 
         SortExpression="type" />
      <asp:BoundField DataField="pub_id" HeaderText="pub_id" 
         SortExpression="pub_id" />
      <asp:BoundField DataField="price" HeaderText="price" 
         SortExpression="price" />
      <asp:BoundField DataField="advance" HeaderText="advance" 
         SortExpression="advance" />
      <asp:BoundField DataField="royalty" HeaderText="royalty" 
         SortExpression="royalty" />
      <asp:BoundField DataField="ytd_sales" HeaderText="ytd_sales" 
         SortExpression="ytd_sales" />
      <asp:BoundField DataField="notes" HeaderText="notes" 
         SortExpression="notes" />
      <asp:BoundField DataField="pubdate" HeaderText="pubdate" 
         SortExpression="pubdate" />

<asp:SqlDataSource ID="SqlDataSource3" runat="server" 
   ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
   SelectCommand="SELECT * FROM [titles] WHERE ([title_id] = @title_id)">
      <asp:ControlParameter ControlID="GridView2" Name="title_id" 
         PropertyName="SelectedValue" Type="String" />

You also need to add in the DataKeyNames attribute to GridView2 and set its value to "title_id":

        <asp:GridView DataKeyNames="title_id" 
           ID="GridView2" runat="server" 

Finally, check the Enable Paging checkbox and apply the Slate scheme (see Figure 16).

Figure 16. Beautifying the DetailsView control

That's it! Press F5 to debug the application. You can now select an author, and then select a title, to display the details of the book in the DetailsView control (see Figure 17).

Figure 17. The application in action!


In this article, you have seen how to use the GridView and DetailsView controls to display records from interrelated tables. And notice that most of the code blocks are written declaratively (in fact, you didn't even write them--they're written by the wizards!).

Wei-Meng Lee (Microsoft MVP) is a technologist and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies.

Return to Windows DevCenter.