How to: Filter Table Rows Using Values from a Parent Table in Dynamic Data
[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
ASP.NET Dynamic Data enables you to use page markup to filter the child table rows to display based on the foreign-key values that the user selects in a parent table. When the user selects a value in the parent table only the child table rows that contain that value are displayed. This lets you perform data filtering without requiring knowledge of the data source control and of the database query details.
The following figure shows the steps that Dynamic Data follows in order to implement table row filtering
-
The parent data-bound control passes the user's selected foreign-key to the QueryExtender control.
-
The QueryExtender control uses this foreign-key value to create the query filtering information. It then passes this filtering information to the child data source control.
-
The child data source control passes the filtering query information to the data source provider.
-
The data source provider passes the query to the database.
-
The database returns the filtered table rows.
-
The data source provider sends this data back to the child data source control.
-
This control finally passes the filtered table rows to the child data-bound control for display.
To filter table rows using values from a parent table
-
In Visual Studio 2010 or Visual Web Developer 2010 Express, in a Dynamic Data Web site, open an ASP.NET Web page. open an ASP.NET Web page that contains a data source control to access the database and a related data-bound control to display the tables. For more information, see Walkthrough: Filtering Table Rows in Dynamic Data.
-
Add a QueryExtender control to the page and set its TargetControlID property to the ID of the data source control that is associated with the child table whose rows you want to filter.
The following example shows the markup for a QueryExtender control that specifies the data source control LinqDataSource1 associated with the child table.
<asp:QueryExtender ID="QueryExtender1" TargetControlID="LinqDataSource1" runat="server"/> </asp:QueryExtender> -
Add a ControlFilterExpression object as a child of the QueryExtender control.
The following example shows the markup for adding a ControlFilterExpression object to the control.
<asp:QueryExtender TargetControlID="LinqDataSource1" runat="server"> <asp:ControlFilterExpression /> </asp:QueryExtender> -
Set the ControlID property of the ControlFilterExpression object to the data-bound control associated with the parent table.
-
Set the Column property of the ControlFilterExpression object to the column in the parent table that is used for filtering. This column represents a foreign key in the child table.
The following example shows the markup for a ControlFilterExpression object that points to a data-bound control and a foreign-key associated with the parent table.
<asp:QueryExtender TargetControlID="LinqDataSource1" runat="server"> <asp:ControlFilterExpression ControlD="ParentGridView" Column="ProductCategory" /> </asp:QueryExtender>
The following example shows how to filter child table rows by using a foreign-key value from a parent table. The example uses the ProductCategories and Products tables from the AdventureWorksLT database. These tables have a master-detail relationship. In the example, the Products table rows are filtered based on the ProductCategory value that is assigned by the user in the ProductCategories table.
This example requires the following:
-
A Dynamic Data Web site or a Dynamic Data Web application. This enables you to create a data context for the database and to create the class for the tables to access.
-
The AdventureWorksLT sample database. For more information, see How to: Connect to the AdventureWorksLT Database using an .MDF File.