RouteParameter 類別

定義

將 URL 區段的值繫結至參數物件。Binds the value of a URL segment to a parameter object.

public ref class RouteParameter : System::Web::UI::WebControls::Parameter
public class RouteParameter : System.Web.UI.WebControls.Parameter
type RouteParameter = class
    inherit Parameter
Public Class RouteParameter
Inherits Parameter
繼承
RouteParameter

範例

本章節中的範例會示範如何使用 RouteParameter 標記中的物件,將 URL 參數值傳遞給針對資料來源控制項所指定的查詢。The examples in this section show how to use RouteParameter objects in markup to pass URL parameter values to a query that is specified for a data source control.

下列範例會顯示 global.asax 頁面中的程式碼,該頁面會定義名為的路由 ProductRouteThe following example shows code in the Global.asax page that defines a route that is named ProductRoute. 路由會定義名為的 URL 參數 productnameThe route defines a URL parameter that is named productname. 路由也會定義一個名為的選擇性「內建」參數 culture ,可用來指定顯示產品描述的語言。The route also defines an optional catchall parameter that is named culture, which can be used to specify the language in which the product description is displayed. Product .aspx 頁面會處理此路由的要求。The Product.aspx page handles requests for this route.

下列範例會顯示 [default.aspx] 頁面。The following example shows the Product.aspx page. 頁面包含的 SqlDataSource 控制項需要兩個參數,一個用於產品名稱,另一個用於產品描述的語言。The page contains a SqlDataSource control that has a query that requires two parameters, one for the name of the product and one for the language of the product description. RouteParameter 物件是用來提供查詢參數的值。RouteParameter objects are used to supply the values for the query parameters. 路由 URL 參數的值 productname 會傳遞給查詢參數 @productnameThe value of the route URL parameter productname is passed to the query parameter @productname. 路由 URL 參數的值 culture 會傳遞給查詢參數 @cultureThe value of the route URL parameter culture is passed to the query parameter @culture. 如果路由 URL 不包含 URL 參數 culture ,則會使用預設值 "en"。If a route URL does not include the URL parameter culture, a default value of "en" is used.

下列範例顯示名為 ProductLinks 的頁面,其中包含使用路由 Url 連結至 default.aspx 頁面的超連結。The following example shows a page that is named ProductLinks.aspx, which contains hyperlinks that link to the Product.aspx page by using route URLs. 這些超連結會使用 RouteUrl 運算式來建立路由 url。These hyperlinks use RouteUrl expressions to create route URLs. 第一個連結不包含 culture 路由參數。The first link does not include a culture route parameter. 因此,在查詢參數的物件中定義的 "en" 預設值 RouteParameter @culture 會用於查詢中。Therefore, the default value of "en" that is defined in the RouteParameter object for @culture query parameter will be used in the query. 第二個連結 culture 會提供 "fr" 的值,以法文顯示描述。The second link provides a culture value of "fr", which causes the description to be displayed in French. 如需如何使用運算式的詳細資訊 RouteUrl ,請參閱 如何:從路由建立 urlFor more information about how to use RouteUrl expressions, see How to: Construct URLs from Routes.

當您按一下第一個超連結時,您會在控制項中看到包含下列值的 default.aspx 頁面 GridViewWhen you click the first hyperlink, you see the Product.aspx page with the following values in the GridView control:

ProductIDProductID 名稱Name 描述Description
994 LL Bottom Bracket Chromoly steel.

當您按一下第二個超連結時,您會在控制項中看到下列值 GridViewWhen you click the second hyperlink, you see the following values in the GridView control:

ProductIDProductID 名稱Name 描述Description
994 LL Bottom Bracket Acier chromé.

若要執行這個範例,請將 global.asax 檔案和 global.asax 檔中的程式碼加入至 ASP.NET 網站或 Web 應用程式,以定義名為的 AdventureWorksLT 資料庫的連接字串 AdventureWorksLTConnectionStringTo run this example, add the two .aspx pages and the code in the Global.asax file to an ASP.NET Web site or Web application that defines a connection string named AdventureWorksLTConnectionString for the AdventureWorksLT database. 如需有關如何設定 AdventureWorksLT 資料庫連接的詳細資訊,請參閱 如何:為 ASP.NET 開發設定 AdventureWorksLT 範例資料庫For information about how to set up a connection to the AdventureWorksLT database, see How to: Set Up an AdventureWorksLT Sample Database for ASP.NET Development.

如需 ASP.NET 路由的詳細資訊,請參閱 ASP.NET 路由For more information about ASP.NET routing, see ASP.NET Routing.

備註

如果您在網站中執行 ASP.NET 路由,可以使用 RouteParameter 物件將所要求 URL 區段的值系結至資料庫查詢或命令。If you implement ASP.NET routing in a Web site, you can use the RouteParameter object to bind the value of a segment of the requested URL to a database query or command. 您可以 RouteParameter 在資料來源控制項或控制項的標記中宣告物件 QueryExtenderYou can declare RouteParameter objects in markup for data source controls or the QueryExtender control.

本主題隨附有原始程式碼的 Visual Studio 網站專案: 下載A Visual Studio Web site project with source code is available to accompany this topic: Download.

建構函式

RouteParameter()

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

RouteParameter(RouteParameter)

使用指定的執行個體值,初始化 RouteParameter 類別的新執行個體。Initializes a new instance of the RouteParameter class by using the values of the specified instance.

RouteParameter(String, DbType, String)

使用指定的參數名稱和資料庫類型,以及使用指定的路由資料索引鍵,初始化 RouteParameter 類別的新執行個體。Initializes a new instance of the RouteParameter class by using the specified name and database type for the parameter, and by using the specified key for the route data.

RouteParameter(String, String)

使用指定的參數名稱以及指定的路由資料索引鍵,初始化 RouteParameter 類別的新執行個體。Initializes a new instance of the RouteParameter class by using the specified name for the parameter and the specified key for route data.

RouteParameter(String, TypeCode, String)

使用指定的參數名稱和型別,以及使用指定的路由資料索引鍵,初始化 RouteParameter 類別的新執行個體。Initializes a new instance of the RouteParameter class by using the specified name and type for the parameter, and by using the specified key for the route data.

屬性

ConvertEmptyStringToNull

取得或設定值,該值指示是否應將 Parameter 物件繫結至的值轉換成 null (如果其為 Empty)。Gets or sets a value indicating whether the value that the Parameter object is bound to should be converted to null if it is Empty.

(繼承來源 Parameter)
DbType

取得或設定參數的資料庫型別。Gets or sets the database type of the parameter.

(繼承來源 Parameter)
DefaultValue

指定參數的預設值,當呼叫 Evaluate(HttpContext, Control) 方法時,要繫結的值應是此參數未初始化的值。Specifies a default value for the parameter, should the value that the parameter is bound to be uninitialized when the Evaluate(HttpContext, Control) method is called.

(繼承來源 Parameter)
Direction

表示此 Parameter 物件是否用來將值繫結至控制項,或是這個控制項是否可用來變更該值。Indicates whether the Parameter object is used to bind a value to a control, or the control can be used to change the value.

(繼承來源 Parameter)
IsTrackingViewState

取得值,指出 Parameter 物件是否正在將變更儲存到它的檢視狀態。Gets a value indicating whether the Parameter object is saving changes to its view state.

(繼承來源 Parameter)
Name

取得或設定參數的名稱。Gets or sets the name of the parameter.

(繼承來源 Parameter)
RouteKey

取得或設定要從其中擷取路由參數值的路由區段名稱。Gets or sets the name of the route segment from which to retrieve the value for the route parameter.

Size

取得或設定參數的大小。Gets or sets the size of the parameter.

(繼承來源 Parameter)
Type

取得或設定參數的類型。Gets or sets the type of the parameter.

(繼承來源 Parameter)
ViewState

取得狀態資訊的字典,允許您在相同頁面的多個要求之間,儲存和還原 Parameter 物件的檢視狀態。Gets a dictionary of state information that allows you to save and restore the view state of a Parameter object across multiple requests for the same page.

(繼承來源 Parameter)

方法

Clone()

傳回目前 RouteParameter 執行個體的複製。Returns a duplicate of the current RouteParameter instance.

Equals(Object)

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

(繼承來源 Object)
Evaluate(HttpContext, Control)

評估要求 URL 並傳回參數的值。Evaluates the request URL and returns the value of the parameter.

GetDatabaseType()

取得 DbType 值,該值等於目前 Parameter 執行個體的 CLR 型別。Gets the DbType value that is equivalent to the CLR type of the current Parameter instance.

(繼承來源 Parameter)
GetHashCode()

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

(繼承來源 Object)
GetType()

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

(繼承來源 Object)
LoadViewState(Object)

將資料來源檢視還原成之前所儲存的檢視狀態。Restores the data source view's previously saved view state.

(繼承來源 Parameter)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
OnParameterChanged()

呼叫 OnParametersChanged(EventArgs) 集合的 ParameterCollection 方法,該集合包含給定的 Parameter 物件。Calls the OnParametersChanged(EventArgs) method of the ParameterCollection collection that contains the Parameter object.

(繼承來源 Parameter)
SaveViewState()

儲存自頁面回傳至伺服器以來 Parameter 物件檢視狀態的變更。Saves the changes to the Parameter object's view state since the time the page was posted back to the server.

(繼承來源 Parameter)
SetDirty()

標記 Parameter 物件,以便將其狀態記錄在檢視狀態中。Marks the Parameter object so its state will be recorded in view state.

(繼承來源 Parameter)
ToString()

將這個執行個體的值轉換為它的相等字串表示。Converts the value of this instance to its equivalent string representation.

(繼承來源 Parameter)
TrackViewState()

會造成 Parameter 物件追蹤其檢視狀態變更,以將這些變更儲存在控制項的 ViewState 物件中,並持續存取相同頁面的其他要求。Causes the Parameter object to track changes to its view state so they can be stored in the control's ViewState object and persisted across requests for the same page.

(繼承來源 Parameter)

明確介面實作

ICloneable.Clone()

傳回目前 Parameter 執行個體的複製。Returns a duplicate of the current Parameter instance.

(繼承來源 Parameter)
IStateManager.IsTrackingViewState

取得值,指出 Parameter 物件是否正在將變更儲存到它的檢視狀態。Gets a value indicating whether the Parameter object is saving changes to its view state.

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

將資料來源檢視還原成之前所儲存的檢視狀態。Restores the data source view's previously saved view state.

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

儲存自頁面回傳至伺服器以來 Parameter 物件檢視狀態的變更。Saves the changes to the Parameter object's view state since the time the page was posted back to the server.

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

會造成 Parameter 物件追蹤其檢視狀態變更,以將這些變更儲存在控制項的 ViewState 物件中,並持續存取相同頁面的其他要求。Causes the Parameter object to track changes to its view state so they can be stored in the control's ViewState object and persisted across requests for the same page.

(繼承來源 Parameter)

適用於

另請參閱