DynamicRouteExpression 類別

定義

從路由資訊取得主索引鍵,以執行資料篩選。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
繼承
DynamicRouteExpression

範例

本主題提供具有原始程式碼的 Visual Studio 專案:動態資料的樣板。A Visual Studio project with source code is available to accompany this topic: Scaffolding Dynamic Data.

下列範例顯示 Products 資料表的自訂清單 .aspx 頁面範本。The following example shows a custom List.aspx page template for the Products table. 它會使用ColumnName屬性來指定用來篩選產品資料列的 ProductCategory 外鍵。It uses the ColumnName property to specify the ProductCategory foreign key for filtering the Products rows.

自訂的 [清單 .aspx] 頁面會顯示包含 ProductCategory 外鍵資料行所包含之類別連結的資料行。The custom List.aspx page displays a column that contains links of the categories that are contained by the ProductCategory foreign-key column. 當使用者按一下其中一個連結時, [清單 .aspx] 頁面只會顯示包含所選類別目錄的資料列。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"
                            />&nbsp;<asp:LinkButton runat="server" CommandName="Delete" Text="Delete"
                                OnClientClick='return confirm("Are you sure you want to delete this item?");'
                            />&nbsp;<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"
                            />&nbsp;<asp:LinkButton runat="server" CommandName="Delete" Text="Delete"
                                OnClientClick='return confirm("Are you sure you want to delete this item?");'
                            />&nbsp;<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

備註

類別會與頁面標記中QueryExtender的控制項搭配使用。 DynamicRouteExpressionThe DynamicRouteExpression class is used with the QueryExtender control in page markup. DynamicRouteExpression類別會從 Web 要求中所包含的路由資訊取得主要索引鍵。The DynamicRouteExpression class gets the primary key from the routing information that is contained in a Web request. 控制項接著會Where在子句中使用該索引鍵。 QueryExtenderThe 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.

您也可以使用您使用ColumnName屬性指定的外鍵來篩選資料。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"/>  

建構函式

DynamicRouteExpression()

初始化 DynamicRouteExpression 類別的新執行個體。Initializes a new instance of the DynamicRouteExpression class.

屬性

ColumnName

取得或設定資料行的名稱,這個資料行包含用來查詢資料來源的外部索引鍵。Gets or sets the name of a column that contains a foreign key that is used to query the data source.

Context

取得擁有者控制項的 HttpContext 執行個體。Gets the HttpContext instance of the owner control.

(繼承來源 DataSourceExpression)
DataSource

取得與擁有者控制項相關聯的資料來源物件。Gets the data source object that is associated with the owner control.

(繼承來源 DataSourceExpression)
IsTrackingViewState

取得值,這個值指出資料來源運算式物件是否正在追蹤其檢視狀態變更。Gets a value that indicates whether a data source expression object is tracking its view state changes.

(繼承來源 DataSourceExpression)
Owner

取得擁有者控制項。Gets the owner control.

(繼承來源 DataSourceExpression)
ViewState

取得 StateBag 類別的執行個體,這個執行個體包含目前的檢視狀態資訊。Gets an instance of the StateBag class that contains the current view state information.

(繼承來源 DataSourceExpression)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetQueryable(IQueryable)

從資料來源取得查詢。Gets the query from the data source.

GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
LoadViewState(Object)

載入 DataSourceExpression 物件中必須保存的值狀態。Loads the state of the values in the DataSourceExpression object that must be persisted.

(繼承來源 DataSourceExpression)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
SaveViewState()

儲存 DataSourceExpression 物件的目前檢視狀態。Saves the current view state of the DataSourceExpression object.

(繼承來源 DataSourceExpression)
SetContext(Control, HttpContext, IQueryableDataSource)

設定 DynamicRouteExpression 物件的 HTTP 內容。Sets the HTTP context of the DynamicRouteExpression object.

SetDirty()

標記 DataSourceExpression 物件,以便將其狀態儲存在檢視狀態中。Marks the DataSourceExpression object so that its state will be saved in view state.

(繼承來源 DataSourceExpression)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)
TrackViewState()

追蹤 DataSourceExpression 物件的檢視狀態變更,以便該變更可針對資料來源運算式物件儲存到 StateBag 物件中。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.

(繼承來源 DataSourceExpression)

明確介面實作

IStateManager.IsTrackingViewState

當由類別實作時取得值,這個值指出資料來源運算式物件是否正在追蹤其檢視狀態變更。When implemented by a class, gets a value that indicates whether a data source expression object is tracking its view state changes.

(繼承來源 DataSourceExpression)
IStateManager.LoadViewState(Object)

當由類別實作時,載入資料來源運算式物件先前儲存的檢視狀態。When implemented by a class, loads the previously saved view state of the data source expression object.

(繼承來源 DataSourceExpression)
IStateManager.SaveViewState()

當類別實作時,儲存 DataSourceExpression 物件的目前檢視狀態。When implemented by a class, saves the current view state of the DataSourceExpression object.

(繼承來源 DataSourceExpression)
IStateManager.TrackViewState()

當由類別實作時,追蹤 DataSourceExpression 物件的檢視狀態變更,以便該變更可針對資料來源運算式物件儲存到 StateBag 物件中。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.

(繼承來源 DataSourceExpression)

適用於