編輯

共用方式為


DynamicRouteExpression Class

Definition

Gets the primary key from the routing information in order to perform data filtering.

public ref class DynamicRouteExpression : System::Web::UI::WebControls::Expressions::DataSourceExpression
public class DynamicRouteExpression : System.Web.UI.WebControls.Expressions.DataSourceExpression
type DynamicRouteExpression = class
    inherit DataSourceExpression
Public Class DynamicRouteExpression
Inherits DataSourceExpression
Inheritance
DynamicRouteExpression

Examples

A Visual Studio project with source code is available to accompany this topic: Scaffolding Dynamic Data.

The following example shows a custom List.aspx page template for the Products table. It uses the ColumnName property to specify the ProductCategory foreign key for filtering the Products rows.

The custom List.aspx page displays a column that contains links of the categories that are contained by the ProductCategory foreign-key column. When the user clicks one of these links, the List.aspx page displays only the rows that contain the selected category.

The example requires the following:

<%@ Page Language="C#" MasterPageFile="~/Site.master" CodeFile="List.aspx.cs" Inherits="List" %>

<%@ Register src="~/DynamicData/Content/GridViewPager.ascx" tagname="GridViewPager" tagprefix="asp" %>

<asp:Content ID="headContent" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:DynamicDataManager ID="DynamicDataManager1" runat="server" AutoLoadForeignKeys="true">
        <DataControls>
            <asp:DataControlReference ControlID="GridView1" />
        </DataControls>
    </asp:DynamicDataManager>

   
    <h2 class="DDSubHeader">Using DynamicRouteExpression with <%= table.DisplayName%> table</h2>

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <div class="DD">
                <asp:ValidationSummary ID="ValidationSummary1" runat="server" EnableClientScript="true"
                    HeaderText="List of validation errors" CssClass="DDValidator" />
                <asp:DynamicValidator runat="server" ID="GridViewValidator" ControlToValidate="GridView1" Display="None" CssClass="DDValidator" />
                <br /> <br />
          
                <div class="DDFilter"> 
                    <asp:HyperLink runat="server" ID="ViewAllLink" Text="View All Categories" />
                </div>
            
            </div>

            <asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource" EnablePersistedSelection="true"
                AllowPaging="True" AllowSorting="True" CssClass="DDGridView"
                RowStyle-CssClass="td" HeaderStyle-CssClass="th" CellPadding="6">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:DynamicHyperLink runat="server" Action="Edit" Text="Edit"
                            /> <asp:LinkButton runat="server" CommandName="Delete" Text="Delete"
                                OnClientClick='return confirm("Are you sure you want to delete this item?");'
                            /> <asp:DynamicHyperLink runat="server" Text="Details" />
                            <!-- Create action link to filter items that
                            belong to the same category -->
                             <a ID="CategoryRouteID" runat="server" 
                                    href='<%# GetRouteInformation() %>'>
                                Filter By: <%# GetProductCategory() %>
                            </a>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>

                <PagerStyle CssClass="DDFooter"/>        
                <PagerTemplate>
                    <asp:GridViewPager runat="server" />
                </PagerTemplate>
                <EmptyDataTemplate>
                    There are currently no items in this table.
                </EmptyDataTemplate>
            </asp:GridView>

            <asp:LinqDataSource ID="GridDataSource" runat="server" EnableDelete="true" />
            
            <asp:QueryExtender TargetControlID="GridDataSource" ID="GridQueryExtender" runat="server">
                <asp:DynamicRouteExpression ColumnName="ProductCategory" />
            </asp:QueryExtender>

            <br />

            <div class="DDBottomHyperLink">
                <asp:DynamicHyperLink ID="InsertHyperLink" runat="server" Action="Insert"><img runat="server" src="~/DynamicData/Content/Images/plus.gif" alt="Insert new item" />Insert new item</asp:DynamicHyperLink>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>
<%@ Page Language="VB" MasterPageFile="~/Site.master" CodeFile="List.aspx.vb" Inherits="List" %>

<%@ Register src="~/DynamicData/Content/GridViewPager.ascx" tagname="GridViewPager" tagprefix="asp" %>

<asp:Content ID="headContent" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:DynamicDataManager ID="DynamicDataManager1" runat="server" AutoLoadForeignKeys="true">
        <DataControls>
            <asp:DataControlReference ControlID="GridView1" />
        </DataControls>
    </asp:DynamicDataManager>

    <h2 class="DDSubHeader"><%= table.DisplayName%></h2>

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <div class="DD">
                <asp:ValidationSummary ID="ValidationSummary1" runat="server" EnableClientScript="true"
                    HeaderText="List of validation errors" CssClass="DDValidator" />
                <asp:DynamicValidator runat="server" ID="GridViewValidator" ControlToValidate="GridView1" Display="None" CssClass="DDValidator" />
            </div>

            <asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource" EnablePersistedSelection="true"
                AllowPaging="True" AllowSorting="True" CssClass="DDGridView"
                RowStyle-CssClass="td" HeaderStyle-CssClass="th" CellPadding="6">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:DynamicHyperLink runat="server" Action="Edit" Text="Edit"
                            /> <asp:LinkButton runat="server" CommandName="Delete" Text="Delete"
                                OnClientClick='return confirm("Are you sure you want to delete this item?");'
                            /> <asp:DynamicHyperLink runat="server" Text="Details" />
                        
                            <!-- Create action link to filter items that
                            belong to the same category -->
                             <a ID="CategoryRouteID" runat="server" 
                                    href='<%# GetRouteInformation() %>'>
                                Filter By: <%# GetProductCategory() %>
                            </a>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>

                <PagerStyle CssClass="DDFooter"/>        
                <PagerTemplate>
                    <asp:GridViewPager runat="server" />
                </PagerTemplate>
                <EmptyDataTemplate>
                    There are currently no items in this table.
                </EmptyDataTemplate>
            </asp:GridView>

            <asp:LinqDataSource ID="GridDataSource" runat="server" EnableDelete="true" />
            
            <!-- Set the foreign-key to use for filtering -->
            <asp:QueryExtender TargetControlID="GridDataSource" ID="GridQueryExtender" runat="server">
                <asp:DynamicRouteExpression ColumnName="ProductCategory" />
            </asp:QueryExtender>

            <br />

            <div class="DDBottomHyperLink">
                <asp:DynamicHyperLink ID="InsertHyperLink" runat="server" Action="Insert"><img runat="server" src="~/DynamicData/Content/Images/plus.gif" alt="Insert new item" />Insert new item</asp:DynamicHyperLink>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>
// Create route information based on the
// foreign-key specified in the 
// DynamicRouteExpression page markup. 
protected string GetRouteInformation()
{

    // Retrieve the current data item.
    var productItem = (Product)GetDataItem();

    if (productItem != null)
    {

        RouteValueDictionary rvd = new RouteValueDictionary();
        rvd.Add("ProductCategoryID", productItem.ProductCategoryID );

        string routePath =
            table.GetActionPath(PageAction.List, rvd);

        return routePath;
    }

    return string.Empty;
}

// Get the name of the foreign-key category. 
protected string GetProductCategory()
{
    // Retrieves the current data item.
    var productItem = (Product)GetDataItem();

    if (productItem != null)
    {
        return productItem.ProductCategory.Name;
    }
    return string.Empty;
}
' Create route information based on the
' foreign-key specified in the 
' DynamicRouteExpression page markup. 
Protected Function GetRouteInformation() As String

    ' Retrieve the current data item.
    Dim productItem = CType(GetDataItem(), Product)

    If productItem IsNot Nothing Then

        Dim rvd As New RouteValueDictionary()
        rvd.Add("ProductCategoryID", productItem.ProductCategoryID)

        Dim routePath As String = table.GetActionPath(PageAction.List, rvd)

        Return routePath

    End If

    Return String.Empty
End Function

' Get the name of the foreign-key category. 
Protected Function GetProductCategory() As String
    ' Retrieves the current data item.
    Dim productItem = CType(GetDataItem(), Product)

    If productItem IsNot Nothing Then
        Return productItem.ProductCategory.Name
    End If
    Return String.Empty
End Function

Remarks

The DynamicRouteExpression class is used with the QueryExtender control in page markup. The DynamicRouteExpression class gets the primary key from the routing information that is contained in a Web request. The QueryExtender control then uses the key in a Where clause. This modifies the query for the data source, which then returns table rows filtered by the key.

You can also filter data by using a foreign key that you specify using the ColumnName property.

Declarative Syntax

<asp:DynamicRouteExpression  
  ColumnName="Name of the foreign key column"/>  

Constructors

DynamicRouteExpression()

Initializes a new instance of the DynamicRouteExpression class.

Properties

ColumnName

Gets or sets the name of a column that contains a foreign key that is used to query the data source.

Context

Gets the HttpContext instance of the owner control.

(Inherited from DataSourceExpression)
DataSource

Gets the data source object that is associated with the owner control.

(Inherited from DataSourceExpression)
IsTrackingViewState

Gets a value that indicates whether a data source expression object is tracking its view state changes.

(Inherited from DataSourceExpression)
Owner

Gets the owner control.

(Inherited from DataSourceExpression)
ViewState

Gets an instance of the StateBag class that contains the current view state information.

(Inherited from DataSourceExpression)

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetQueryable(IQueryable)

Gets the query from the data source.

GetType()

Gets the Type of the current instance.

(Inherited from Object)
LoadViewState(Object)

Loads the state of the values in the DataSourceExpression object that must be persisted.

(Inherited from DataSourceExpression)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
SaveViewState()

Saves the current view state of the DataSourceExpression object.

(Inherited from DataSourceExpression)
SetContext(Control, HttpContext, IQueryableDataSource)

Sets the HTTP context of the DynamicRouteExpression object.

SetDirty()

Marks the DataSourceExpression object so that its state will be saved in view state.

(Inherited from DataSourceExpression)
ToString()

Returns a string that represents the current object.

(Inherited from Object)
TrackViewState()

Tracks view state changes of the DataSourceExpression object so that the changes can be stored in the StateBag object for the data source expression object.

(Inherited from DataSourceExpression)

Explicit Interface Implementations

IStateManager.IsTrackingViewState

When implemented by a class, gets a value that indicates whether a data source expression object is tracking its view state changes.

(Inherited from DataSourceExpression)
IStateManager.LoadViewState(Object)

When implemented by a class, loads the previously saved view state of the data source expression object.

(Inherited from DataSourceExpression)
IStateManager.SaveViewState()

When implemented by a class, saves the current view state of the DataSourceExpression object.

(Inherited from DataSourceExpression)
IStateManager.TrackViewState()

When implemented by a class, tracks view state changes of the DataSourceExpression object so that the changes can be stored in the StateBag object for the data source expression object.

(Inherited from DataSourceExpression)

Applies to