MenuItemBinding MenuItemBinding MenuItemBinding MenuItemBinding Class

定義

Menu 控制項中,定義資料項目及其繫結功能表項目之間的關係。Defines the relationship between a data item and the menu item it is binding to in a Menu control. 這個類別無法被繼承。This class cannot be inherited.

public ref class MenuItemBinding sealed : ICloneable, System::Web::UI::IDataSourceViewSchemaAccessor, System::Web::UI::IStateManager
public sealed class MenuItemBinding : ICloneable, System.Web.UI.IDataSourceViewSchemaAccessor, System.Web.UI.IStateManager
type MenuItemBinding = class
    interface IStateManager
    interface ICloneable
    interface IDataSourceViewSchemaAccessor
Public NotInheritable Class MenuItemBinding
Implements ICloneable, IDataSourceViewSchemaAccessor, IStateManager
繼承
MenuItemBindingMenuItemBindingMenuItemBindingMenuItemBinding
實作

範例

下列程式碼範例示範如何使用MenuItemBinding物件來定義XmlDataSource控制項的欄位與Menu控制項中的功能表項目之間的關聯性。The following code example demonstrates how to use MenuItemBinding objects to define the relationship between the fields of an XmlDataSource control and the menu items in a Menu control. 為了讓此範例正確運作, 您必須將下列範例 XML 資料複製到名為 Menu .xml 的檔案。For this example to work correctly, you must copy the sample XML data below to a file named Menu.xml.


<%@ page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemBinding Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>MenuItemBinding Example</h3>

      <asp:menu id="NavigationMenu"
        datasourceid="MenuSource"
        runat="server">
        
        <DataBindings>
        
          <asp:menuitembinding datamember="MapHomeNode"
            formatstring="({0})" 
            textfield="Title"
            valuefield="Description"
            imageurlfield="ImageUrl"
            tooltipfield="ToolTip"
            target="_self" />
          <asp:menuitembinding datamember="MapNode" 
            depth="1"
            formatstring="[{0}]" 
            textfield="Title"
            valuefield="Description"
            imageurlfield="ImageUrl"
            tooltipfield="ToolTip"
            target="_blank"/>
          <asp:menuitembinding datamember="MapNode" 
            depth="2"
            formatstring="<{0}>" 
            textfield="Title"
            valuefield="Description"
            imageurlfield="ImageUrl"
            tooltipfield="ToolTip"
            target="_blank"/>
          
        </DataBindings>
        
      </asp:menu>

      <asp:xmldatasource id="MenuSource"
        datafile="Menu.xml"
        runat="server"/> 

    </form>
  </body>
</html>


<%@ page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemBinding Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>MenuItemBinding Example</h3>

      <asp:menu id="NavigationMenu"
        datasourceid="MenuSource"
        runat="server">
        
        <DataBindings>
        
          <asp:menuitembinding datamember="MapHomeNode"
            formatstring="({0})" 
            textfield="Title"
            valuefield="Description"
            imageurlfield="ImageUrl"
            tooltipfield="ToolTip"
            target="_self" />
          <asp:menuitembinding datamember="MapNode" 
            depth="1"
            formatstring="[{0}]" 
            textfield="Title"
            valuefield="Description"
            imageurlfield="ImageUrl"
            tooltipfield="ToolTip"
            target="_blank"/>
          <asp:menuitembinding datamember="MapNode" 
            depth="2"
            formatstring="<{0}>" 
            textfield="Title"
            valuefield="Description"
            imageurlfield="ImageUrl"
            tooltipfield="ToolTip"
            target="_blank"/>
          
        </DataBindings>
        
      </asp:menu>

      <asp:xmldatasource id="MenuSource"
        datafile="Menu.xml"
        runat="server"/> 

    </form>
  </body>
</html>

下列程式碼是上一個範例的範例網站地圖資料。The following code is sample site map data for the previous example.

<MapHomeNode ImageUrl="~\Images\Home.gif"

Title="Home"

Description="Root Page"

ToolTip="Home Page">

<MapNode ImageUrl="~\Images\Music.gif"

Title="Music"

Description="Music Category"

ToolTip="Music Page">

<MapNode ImageUrl="~\Images\Classical.gif"

Title="Classical"

Description="Classical Section"

ToolTip="Classical Page"/>

<MapNode ImageUrl="~\Images\Rock.gif"

Title="Rock"

Description="Rock Section"

ToolTip="Rock Page"/>

<MapNode ImageUrl="~\Images\Jazz.gif"

Title="Jazz"

Description="Jazz Section"

ToolTip="Jazz Page"/>

</MapNode>

<MapNode ImageUrl="~\Images\Movies.gif"

Title="Movies"

Description="Movies Category"

ToolTip="Movies Page">

<MapNode ImageUrl="~\Images\Action.gif"

Title="Action"

Description="Action Section"

ToolTip="Action Page"/>

<MapNode ImageUrl="~\Images\Drama.gif"

Title="Drama"

Description="Drama Section"

ToolTip="Drama Page"/>

<MapNode ImageUrl="~\Images\Musical.gif"

Title="Musical"

Description="Musical Section"

ToolTip="Musical Page"/>

</MapNode>

</MapHomeNode>

備註

當控制項系結至每個資料項目包含多個欄位的資料來源時 (例如具有數個屬性的 XML 元素), 如果未定義任何功能表項目系結, 則功能表項目會顯示ToString()由的方法所傳回的值。 Menu預設的資料項目。When the Menu control is bound to a data source where each data item contains multiple fields (such as an XML element with several attributes), if no menu item bindings are defined, a menu item displays the value returned by the ToString() method of the data item by default. 在 XML 專案的案例中, 功能表項目會顯示元素名稱, 這會顯示功能表的基礎結構, 但不是非常實用。In the case of an XML element, the menu item displays the element name, which shows the underlying structure of the menu, but is not very useful otherwise. 您可以藉由指定功能表項目系結, 將功能表項目的屬性系結至特定欄位。You can bind the properties of a menu item to a specific field by specifying menu item bindings. MenuItemBinding物件會定義每個資料項目與所系結之功能表項目之間的關聯性。A MenuItemBinding object defines the relationship between each data item and the menu item it is binding to.

注意

當控制項系結至控制項時, 功能表項目系結不會有任何作用。 SiteMapDataSource MenuWhen the Menu control is bound to a SiteMapDataSource control, menu item bindings have no effect. 系統會使用網站地圖提供者自動執行系結。Binding is performed automatically using the site map provider.

控制項會將其MenuItemBinding物件儲存在DataBindings屬性中, 並將系結套用至資料來源, 以在功能表階層和資料來源階層之間建立一對一關聯性。 MenuThe Menu control stores its MenuItemBinding objects in the DataBindings property and applies the bindings to the data source to create a one-to-one relationship between the menu hierarchy and the data source hierarchy. 對於資料來源中的每個資料項目, Menu控制項會嘗試將資料項目MenuItemBinding與物件進行比對, 以建立對應MenuItem的物件。For each data item in the data source, the Menu control attempts to match the data item to a MenuItemBinding object to create the corresponding MenuItem object.

建立MenuItemBinding物件時, 您必須指定系結的準則。When creating a MenuItemBinding object, you must specify the criteria for binding. 準則會指出資料項目何時應系結至功能表項目。The criteria indicate when a data item should be bound to a menu item. 您可以指定Depth DataMember、或兩者。You can specify the Depth, the DataMember, or both.

功能表項目深度會指定要系結的功能表層級。A menu item depth specifies the menu level that gets bound. 例如, 下列MenuItemBinding宣告會將資料來源的名稱和識別碼欄位分別系結Text至和Value屬性 (深度為0的所有節點):For example, the following MenuItemBinding declaration binds the Name and ID fields of the data source to the Text and Value properties, respectively, of all nodes with a depth of 0:

<asp:MenuItemBinding Depth="0" TextField="Name" ValueField="ID">  

資料成員會指定基礎資料來源中資料項目的類型, 但是可以根據資料來源來代表不同的資訊。A data member specifies the type of the data item in the underlying data source, but can represent different information depending on the data source. 階層式資料來源中的每個資料項目 (由IHierarchyData物件表示) 都會Type公開屬性, 以指定資料項目的類型。Each data item in a hierarchical data source (represented by an IHierarchyData object) exposes a Type property, which specifies the type of the data item. 例如, XML 元素的資料成員會指定元素的名稱。For example, the data member for an XML element specifies the name of the element. 當資料來源包含多個資料項目類型時, 資料成員會指定要使用的資料項目類型。When a data source contains multiple data item types, the data member specifies which data item type to use. 下列MenuItemBinding宣告會將XmlDataSource控制項<Book>的元素系結至功能表中的所有功能表項目, 而不論階層中的位置為何:The following MenuItemBinding declaration binds the <Book> elements of an XmlDataSource control to all the menu items in the menu, regardless of the location in the hierarchy:

<asp:MenuItemBinding DataMember="Book" TextField="Title" ValueField= "ISBN">  

有時候您可能需要建立同時指定深度和資料成員的功能表項目系結。Sometimes you might need to create a menu item binding that specifies both a depth and a data member. 當資料來源包含的專案位於具有相同資料成員值的不同層級時, 通常會使用此選項。This is often used when the data source contains items at different levels that have the same data member value. 例如, 您可以讓<Item>元素出現在 XML 檔案中的不同層級。For example, you can have <Item> elements that appear at different levels within an XML file. 下列MenuItemBinding宣告顯示如何指定在不同功能表深度套用至相同資料成員的功能表項目系結:The following MenuItemBinding declarations show how to specify menu item bindings that apply to identical data members at different menu depths:

<asp:MenuItemBinding DataMember="Item" Depth="1" TextField="Title">

<asp:MenuItemBinding DataMember="Item" Depth="2" TextField="ISBN">

如果定義了沒有深度和資料成員的功能表項目系結, 則功能表項目系結會套用至功能表中的所有功能表項目。If a menu item binding is defined without a depth and a data member, the menu item binding is applied to all menu items within the menu. 當所有資料項目都具有相同的屬性, 而且不論功能表深度為何, 都應該以相同的方式顯示, 通常會使用此選項。This is commonly used when all data items have the same properties and should be displayed identically, regardless of the menu depth.

建立系結準則之後, 您可以接著系結可系結至MenuItem值之物件的屬性。After the binding criteria are established, you can then bind a property of a MenuItem object that can be bound to a value. 您可以系結至資料項目的欄位, 或系結至靜態值。You can bind to a field of a data item or to a static value. 當系結至靜態值時, MenuItem套用該MenuItemBinding物件的所有物件都會共用相同的值。When bound to a static value, all MenuItem objects to which the MenuItemBinding object is applied share the same value. 系結至欄位的屬性包含資料來源中對應欄位的值。Properties bound to fields contain the values of the corresponding field from the data source.

注意

您可以藉由直接設定對應的屬性MenuItem , 選擇性地覆寫物件中的系結屬性。You can selectively override a bound property in a MenuItem object by setting the corresponding property directly.

下表列出MenuItemBinding類別的屬性, 可讓您將MenuItem物件的屬性系結至資料項目的欄位。The following table lists the properties of the MenuItemBinding class that allow you to bind a property of the MenuItem object to a field of a data item.

屬性Property 說明Description
ImageUrlField 要系結至ImageUrl MenuItem物件之屬性的欄位。The field to bind to the ImageUrl property of a MenuItem object.
NavigateUrlField 要系結至NavigateUrl MenuItem物件之屬性的欄位。The field to bind to the NavigateUrl property of a MenuItem object.
TextField 要系結至Text MenuItem物件之屬性的欄位。The field to bind to the Text property of a MenuItem object.
ToolTipField 要系結至ToolTip MenuItem物件之屬性的欄位。The field to bind to the ToolTip property of a MenuItem object.
ValueField 要系結至Value MenuItem物件之屬性的欄位。The field to bind to the Value property of a MenuItem object.

下表列出MenuItemBinding類別的屬性, 可讓您將MenuItem物件的屬性系結至靜態值。The following table lists the properties of the MenuItemBinding class that allow you to bind a property of the MenuItem object to a static value.

屬性Property 說明Description
ImageUrl 要系結至ImageUrl MenuItem物件之屬性的靜態值。The static value to bind to the ImageUrl property of a MenuItem object.
NavigateUrl 要系結至NavigateUrl MenuItem物件之屬性的靜態值。The static value to bind to the NavigateUrl property of a MenuItem object.
Target 要系結至Target MenuItem物件之屬性的靜態值。The static value to bind to the Target property of a MenuItem object.
Text 要系結至Text MenuItem物件之屬性的靜態值。The static value to bind to the Text property of a MenuItem object.
ToolTip 要系結至ToolTip MenuItem物件之屬性的靜態值。The static value to bind to the ToolTip property of a MenuItem object.
Value 要系結至Value MenuItem物件之屬性的靜態值。The static value to bind to the Value property of a MenuItem object.

如果定義MenuItemBinding了衝突的物件Menu , 控制項就會依照下列優先順序套用功能表項目系結:If conflicting MenuItemBinding objects are defined, the Menu control applies the menu item bindings in the following order of precedence:

  1. 定義MenuItemBinding和同時符合深度和資料成員的物件。The MenuItemBinding object that defines and matches both a depth and a data member.

  2. 定義和只符合資料成員的物件。MenuItemBindingThe MenuItemBinding object that defines and matches the data member only.

  3. 定義並僅符合深度的物件。MenuItemBindingThe MenuItemBinding object that defines and matches the depth only.

  4. 不定義深度或資料成員的物件。MenuItemBindingThe MenuItemBinding object that defines neither the depth nor the data member. (這種類型的功能表項目系結會套用至功能表中的所有功能表項目)。(This type of menu item binding is applied to all menu items in the menu.)

  5. 在資料來源中沒有相符的物件。MenuItemBindingThe MenuItemBinding object that does not have a match in the data source. 在此情況下ToString() , 資料項目目的方法所傳回的值會接著系結Text至套用該Value MenuItemBinding物件之功能表項目的和屬性。In this case, the value returned by the ToString() method of the data item is then bound to the Text and Value properties of the menu items to which the MenuItemBinding object is applied.

類別也可讓您藉由FormatString設定屬性來格式化功能表項目中顯示的文字。 MenuItemBindingThe MenuItemBinding class also allows you to format the text displayed in a menu item by setting the FormatString property.

建構函式

MenuItemBinding() MenuItemBinding() MenuItemBinding() MenuItemBinding()

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

屬性

DataMember DataMember DataMember DataMember

取得或設定要繫結至功能表項目的資料成員。Gets or sets the data member to bind to a menu item.

Depth Depth Depth Depth

取得或設定套用 MenuItemBinding 物件的功能表深度。Gets or sets the menu depth to which the MenuItemBinding object is applied.

Enabled Enabled Enabled Enabled

取得或設定值,指出套用 MenuItemBinding 物件的功能表項目是否已啟用,讓項目能夠顯示快顯影像及任何子功能表項目。Gets or sets a value that indicates whether the menu item to which the MenuItemBinding object is applied is enabled, allowing the item to display a pop-out image and any child menu items.

EnabledField EnabledField EnabledField EnabledField

取得或設定欄位來自資料來源之欄位的名稱,以繫結至套用 Enabled 物件之 MenuItem 物件的 MenuItemBinding 物件。Gets or sets the name of the field from the data source to bind to the Enabled property of a MenuItem object to which the MenuItemBinding object is applied.

FormatString FormatString FormatString FormatString

取得或設定字串,指定套用 MenuItemBinding 物件之功能表項目文字的顯示格式。Gets or sets the string that specifies the display format for the text of a menu item to which the MenuItemBinding object is applied.

ImageUrl ImageUrl ImageUrl ImageUrl

取得或設定影像的 URL,該影像會顯示在套用 MenuItemBinding 物件之功能表項目的文字旁。Gets or sets the URL to an image that is displayed next to the text of a menu item to which the MenuItemBinding object is applied.

ImageUrlField ImageUrlField ImageUrlField ImageUrlField

取得或設定欄位來自資料來源之欄位的名稱,以繫結至套用 ImageUrl 物件之 MenuItem 物件的 MenuItemBinding 物件。Gets or sets the name of the field from the data source to bind to the ImageUrl property of a MenuItem object to which the MenuItemBinding object is applied.

NavigateUrl NavigateUrl NavigateUrl NavigateUrl

取得或設定在按一下套用 MenuItemBinding 物件的功能表項目時連結至的 URL。Gets or sets the URL to link to when a menu item to which the MenuItemBinding object is applied is clicked.

NavigateUrlField NavigateUrlField NavigateUrlField NavigateUrlField

取得或設定欄位來自資料來源之欄位的名稱,以繫結至套用 NavigateUrl 物件之 MenuItem 物件的 MenuItemBinding 物件。Gets or sets the name of the field from the data source to bind to the NavigateUrl property of a MenuItem object to which the MenuItemBinding object is applied.

PopOutImageUrl PopOutImageUrl PopOutImageUrl PopOutImageUrl

取得或設定影像的 URL,該影像表示套用 MenuItemBinding 物件的功能表項目是否有動態 (Dynamic) 子功能表。Gets or sets the URL to an image that indicates the presence of a dynamic submenu for a menu item to which the MenuItemBinding object is applied.

PopOutImageUrlField PopOutImageUrlField PopOutImageUrlField PopOutImageUrlField

取得或設定欄位來自資料來源之欄位的名稱,以繫結至套用 PopOutImageUrl 物件之 MenuItem 物件的 MenuItemBinding 物件。Gets or sets the name of the field from the data source to bind to the PopOutImageUrl property of a MenuItem object to which the MenuItemBinding object is applied.

Selectable Selectable Selectable Selectable

取得或設定值,指出套用 MenuItemBinding 物件的功能表項目是否可選取,或是否為「可點選」。Gets or sets a value that indicates whether the menu item to which the MenuItemBinding object is applied can be selected, or is "clickable."

SelectableField SelectableField SelectableField SelectableField

取得或設定欄位來自資料來源之欄位的名稱,以繫結至套用 Selectable 物件之 MenuItem 物件的 MenuItemBinding 物件。Gets or sets the name of the field from the data source to bind to the Selectable property of a MenuItem object to which the MenuItemBinding object is applied.

SeparatorImageUrl SeparatorImageUrl SeparatorImageUrl SeparatorImageUrl

取得或設定影像的 URL,對於套用 MenuItemBinding 物件的功能表項目,該影像會顯示於功能表項目的文字下方 (以將其本身與其他功能表項目加以分隔)。Gets or sets the URL to an image displayed below the text of a menu item (to separate it from other menu items) for a menu item to which the MenuItemBinding object is applied.

SeparatorImageUrlField SeparatorImageUrlField SeparatorImageUrlField SeparatorImageUrlField

取得或設定欄位來自資料來源之欄位的名稱,以繫結至套用 SeparatorImageUrl 物件之 MenuItem 物件的 MenuItemBinding 物件。Gets or sets the name of the field from the data source to bind to the SeparatorImageUrl property of a MenuItem object to which the MenuItemBinding object is applied.

Target Target Target Target

取得或設定目標視窗或框架,在其中顯示與套用 MenuItemBinding 物件之功能表項目相關聯的 Web 網頁內容。Gets or sets the target window or frame in which to display the Web page content associated with a menu item to which the MenuItemBinding object is applied.

TargetField TargetField TargetField TargetField

取得或設定欄位來自資料來源之欄位的名稱,以繫結至套用 Target 物件之 MenuItem 物件的 MenuItemBinding 物件。Gets or sets the name of the field from the data source to bind to the Target property of a MenuItem object to which the MenuItemBinding object is applied.

Text Text Text Text

取得或設定為套用 MenuItemBinding 物件之功能表項目所顯示的文字。Gets or sets the text displayed for the menu item to which the MenuItemBinding object is applied.

TextField TextField TextField TextField

取得或設定欄位來自資料來源之欄位的名稱,以繫結至套用 Text 物件之 MenuItem 物件的 MenuItemBinding 物件。Gets or sets the name of the field from the data source to bind to the Text property of a MenuItem object to which the MenuItemBinding object is applied.

ToolTip ToolTip ToolTip ToolTip

取得或設定套用 MenuItemBinding 物件之功能表項目的工具提示文字。Gets or sets the ToolTip text for a menu item to which the MenuItemBinding object is applied.

ToolTipField ToolTipField ToolTipField ToolTipField

取得或設定欄位來自資料來源之欄位的名稱,以繫結至套用 ToolTip 物件之 MenuItem 物件的 MenuItemBinding 物件。Gets or sets the name of the field from the data source to bind to the ToolTip property of a MenuItem object to which the MenuItemBinding object is applied.

Value Value Value Value

取得或設定非顯示值,這個值是用來儲存與套用 MenuItemBinding 物件之功能表項目相關的任何其他資料,例如,用於處理回傳事件的資料。Gets or sets a nondisplayed value used to store any additional data about a menu item to which the MenuItemBinding object is applied, such as data used for handling postback events.

ValueField ValueField ValueField ValueField

取得或設定欄位來自資料來源之欄位的名稱,以繫結至套用 Value 物件之 MenuItem 物件的 MenuItemBinding 物件。Gets or sets the name of the field from the data source to bind to the Value property of a MenuItem object to which the MenuItemBinding object is applied.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
ToString() ToString() ToString() ToString()

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

明確介面實作

ICloneable.Clone() ICloneable.Clone() ICloneable.Clone() ICloneable.Clone()

建立 MenuItemBinding 物件的複本。Creates a copy of the MenuItemBinding object.

IDataSourceViewSchemaAccessor.DataSourceViewSchema IDataSourceViewSchemaAccessor.DataSourceViewSchema IDataSourceViewSchemaAccessor.DataSourceViewSchema IDataSourceViewSchemaAccessor.DataSourceViewSchema

如需這個成員的說明,請參閱 DataSourceViewSchemaFor a description of this member, see DataSourceViewSchema.

IStateManager.IsTrackingViewState IStateManager.IsTrackingViewState IStateManager.IsTrackingViewState IStateManager.IsTrackingViewState

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

IStateManager.LoadViewState(Object) IStateManager.LoadViewState(Object) IStateManager.LoadViewState(Object) IStateManager.LoadViewState(Object)

載入節點先前儲存的檢視狀態。Loads the node's previously saved view state.

IStateManager.SaveViewState() IStateManager.SaveViewState() IStateManager.SaveViewState() IStateManager.SaveViewState()

將檢視狀態變更儲存至 ObjectSaves the view state changes to an Object.

IStateManager.TrackViewState() IStateManager.TrackViewState() IStateManager.TrackViewState() IStateManager.TrackViewState()

指示 MenuItemBinding 物件追蹤其檢視狀態的變更。Instructs the MenuItemBinding object to track changes to its view state.

適用於

另請參閱