In Solution Explorer, right-click the project name, and then select Add New Item.
Under Visual Studio installed templates, select Web Form.
In the Name box, enter ForeignKeyTableRowFiltering.aspx. Make sure that you select Place code in separate file.
Add a DynamicDataManager control to the page, as shown in the following example:
<body>
<form ID="Form1" runat="server">
<asp:DynamicDataManager ID="DynamicDataManager1"
runat="server"/>
</form>
</body>
The DynamicDataManager control must be included on a page in order to support Dynamic Data controls. The markup for the DynamicDataManager control must precede the markup for any controls that use Dynamic Data.
From the Data tab of the Toolbox, add a LinqDataSource control to the page, as shown in the following example:
<form ID="Form1" runat="server">
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
</asp:LinqDataSource>
</form>
Set the LinqDataSource control's TableName property to the database table to access (Products), as shown in the following example:
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
TableName="Products"
</asp:LinqDataSource>
Set the ContextTypeName property to the data context class (AdventureWorksLTDataContext), as shown in the following example:
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="AdventureWorksLTDataContext">
</asp:LinqDataSource>
From the Data tab of the Toolbox, add a GridView control to the page.
This control will display the Products table data fields. Set the DataSourceID property to the ID of the LinqDataSource control, as shown in the following example:
<form ID="Form1" runat="server" >
<asp:GridView ID="GridView1" runat="server"
DataSourceID="LinqDataSource1" >
</asp:GridView>
</form>
Set the GridView control's AutoGenerateColumns property to false, as shown in the following example:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="false" >
</asp:GridView>
This disables the automatic generation of columns that are based on the database table. Instead you will use a DynamicField controls to populate the GridView control.
Optionally, set the GridView control's AllowPaging and AllowSorting properties to true, as shown in the following example:
<asp:GridView ID="GridView1" runat="server"
AllowPaging="true"
AllowSorting="true" >
</asp:GridView>
Add DynamicField controls to the Columns property and set their DataField properties to "ProductCategory", "Name", "Color", and "Size".
DynamicField controls use ASP.NET Dynamic Data to read data from the data model and to format it by using the appropriate field templates. The following example shows the markup for the Columns element and the DynamicField controls:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:DynamicField DataField="ProductCategory" />
<asp:DynamicField DataField="Name" />
<asp:DynamicField DataField="Color" />
<asp:DynamicField DataField="Size" />
</Columns>
</asp:GridView>
Register the GridView control with the DynamicDataManager control, as shown in the following example:
<asp:DynamicDataManager ID="DynamicDataManager1"
runat="server">
<DataControls>
<asp:DataControlReference ControlID="GridView1"/>
</DataControls>
</asp:DynamicDataManager>
In the markup above the data-bound control, add a DynamicFilter control to the page.
Set the DataField property of the DynamicFilter control toProductCategory.
This is the name of the column to use to for table row filtering. The following example shows the markup for the DynamicFilter control.
<asp:DynamicFilter ID="DynamicFilter1"
DataField="ProductCategory" runat="server"/>
Dynamic Data throws an InvalidOperationException error if the column type does not have an associated filter template.
In the markup above the DynamicFilter control, add a Label control, as shown in the following example:
<asp:Label ID="Label1"
Text="ProductCategory " runat="server"/>
You will use this Label control to display the name of the column that is used for filtering.
Add a QueryExtender control to the page.
Set the TargetControlID property of QueryExtender control to the ID of the data source control that you want to extend, as shown in the following example:
<asp:QueryExtender ID="QueryExtenderID"
TargetControlID="LinqDataSource1" runat="server"/>
</asp:QueryExtender>
The QueryExtender control extends a data source control's capabilities by letting you configure data filtering through declarative syntax, as shown in the next steps.
Add a DynamicFilterExpression object as a child of the QueryExtender control.
Set the ControlID property of the DynamicFilterExpression object to the identifier of the DynamicFilter control, as shown in the following example:
<asp:QueryExtender ID="QueryExtenderID"
TargetControlID="LinqDataSource1" runat="server">
<asp:DynamicFilterExpression ControlID="DynamicFilter1"/>
</asp:QueryExtender>
Save and close the file.
In Solution Explorer, right-click the ForeignKeyTableRowFiltering.aspx page and then select View in Browser.
The page displays the columns from the Products and the UI for filtering the products by category.
In the Product Category drop-down control, select a category.
Dynamic Data filters the products that are displayed, based on the category that you selected.
Close the ForeignKeyTableRowFiltering.aspx page.