Menu.DataBindings 屬性

定義

取得 MenuItemBinding 物件的集合,這些物件會定義資料項目及它所繫結之功能表項目之間的關係。Gets a collection of MenuItemBinding objects that define the relationship between a data item and the menu item it is binding to.

public:
 property System::Web::UI::WebControls::MenuItemBindingCollection ^ DataBindings { System::Web::UI::WebControls::MenuItemBindingCollection ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public System.Web.UI.WebControls.MenuItemBindingCollection DataBindings { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.DataBindings : System.Web.UI.WebControls.MenuItemBindingCollection
Public ReadOnly Property DataBindings As MenuItemBindingCollection

屬性值

MenuItemBindingCollection

MenuItemBindingCollection,代表資料項目及它所繫結之功能表項目之間的關係。A MenuItemBindingCollection that represents the relationship between a data item and the menu item it is binding to.

屬性

範例

下列程式碼範例示範如何使用 DataBindings 集合來定義控制項的欄位 XmlDataSource 與控制項中的功能表項目之間的關聯性 MenuThe following code example demonstrates how to use the DataBindings collection to define the relationship between the fields of an XmlDataSource control and the menu items in a Menu control. 為了讓此範例正確運作,您必須將下列範例 XML 資料複製到名為 Map.xml 的檔案。For this example to work correctly, you must copy the sample XML data below to a file named Map.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>Menu DataBindings Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>Menu DataBindings Example</h3>
    
      <asp:menu id="NavigationMenu"
        staticdisplaylevels="1"
        staticsubmenuindent="10" 
        orientation="Vertical"
        target="_blank"
        datasourceid="MenuSource"
        runat="server">
        
       <DataBindings>
        
          <asp:menuitembinding datamember="MapHomeNode" 
            depth="0"
            textfield="title" 
            navigateurlfield="url"/>
          <asp:menuitembinding datamember="MapNode" 
            depth="1"
            textfield="title" 
            navigateurlfield="url"/>
          <asp:menuitembinding datamember="MapNode" 
            depth="2"
            textfield="title" 
            navigateurlfield="url"/>
        </DataBindings>
        
      </asp:menu>
      
      <asp:XmlDataSource id="MenuSource"
        datafile="Map.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>Menu DataBindings Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>Menu DataBindings Example</h3>
    
      <asp:menu id="NavigationMenu"
        staticdisplaylevels="1"
        staticsubmenuindent="10" 
        orientation="Vertical"
        target="_blank"
        datasourceid="MenuSource"
        runat="server">
        
        <DataBindings>
          <asp:menuitembinding datamember="MapHomeNode" 
            depth="0"
            textfield="title" 
            navigateurlfield="url"/>
          <asp:menuitembinding datamember="MapNode" 
            depth="1"
            textfield="title" 
            navigateurlfield="url"/>
          <asp:menuitembinding datamember="MapNode" 
            depth="2"
            textfield="title" 
            navigateurlfield="url"/>
        </DataBindings>
        
      </asp:menu>
      
      <asp:XmlDataSource id="MenuSource"
        datafile="Map.xml"
        runat="server"/>        

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

以下是前一個範例的範例網站地圖資料。The following is sample site map data for the previous example.

<MapHomeNode url="~\Home.aspx"   
  title="Home"  
  description="Home">  
  <MapNode url="~\Music.aspx"  
    title="Music"  
    description="Music">  
    <MapNode url="~\Classical.aspx"   
      title="Classical"  
      description="Classical"/>  
    <MapNode url="~\Rock.aspx"  
      title="Rock"  
      description="Rock"/>  
    <MapNode url="~\Jazz.aspx"  
      title="Jazz"  
      description="Jazz"/>  
  </MapNode>  
  <MapNode url="~\Movies.aspx"  
    title="Movies"  
    description="Movies">  
    <MapNode url="~\Action.aspx"  
      title="Action"  
      description="Action"/>  
    <MapNode url="~\Drama.aspx"  
      title="Drama"  
      description="Drama"/>  
    <MapNode url="~\Musical.aspx"  
      title="Musical"  
      description="Musical"/>  
  </MapNode>  
</MapHomeNode>  

備註

DataBindings集合包含 MenuItemBinding 的物件,會定義資料項目與其系結之功能表項目之間的關聯性。The DataBindings collection contains MenuItemBinding objects that define the relationship between a data item and the menu item it is binding to. 當系結至每個資料項目包含多個屬性 (的資料來源時(例如具有數個屬性) 的 XML 元素),功能表項目預設會顯示資料項目的方法所傳回的值 ToString()When binding to a data source where each data item contains multiple properties (such as an XML element with several attributes), 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 tree, but is not very useful otherwise. 您可以藉由指定功能表項目系結,將功能表項目系結至特定的資料項目屬性。You can bind a menu item to a specific data item property by specifying menu item bindings.

定義資料項目與功能表項目之間的關聯性時,您必須同時指定系結的準則,以及要系結之資料項目的屬性。When defining the relationship between a data item and a menu item, you must specify both the criteria for binding and the property of a data item to bind to. 準則會指出資料項目何時應系結至功能表項目。The criteria indicate when a data item should be bound to a menu item. 您可以使用深度、資料成員或兩者來指定準則。The criteria can be specified with a depth, a data member, or both. 深度會指定要系結的功能表層級。The depth specifies the menu level that gets bound. 例如,如果您指定深度為0,則在層級0的樹狀結構中的所有功能表項目都會使用功能表項目系結來系結。For example, if you specify a depth of 0, all menu items in the tree structure at level 0 are bound using the menu item binding. 資料成員會指定基礎資料來源中資料項目的類型,但是可以根據資料來源來代表不同的資訊。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. 例如,XML 元素的資料成員會指定元素的名稱。For example, the data member for an XML element specifies the name of the element.

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

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

  2. MenuItemBinding僅定義深度的物件。The MenuItemBinding object that defines only the depth.

  3. MenuItemBinding僅定義資料成員的物件。The MenuItemBinding object that defines only the data member.

  4. MenuItemBinding不定義深度或資料成員的物件。The MenuItemBinding object that defines neither the depth nor the data member.

建立系結準則之後,您就可以系結能夠系結 MenuItem 至資料項目之屬性或欄位的物件屬性。Once the binding criteria are established, you can then bind a property of a MenuItem object that is able to be bound to an attribute or field of a data item. 例如,您可以藉 Text 由設定物件的屬性,將功能表項目的屬性系結至 XML 元素上的 text 屬性 TextField MenuItemBindingFor example, you can bind the Text property of a menu item to the text attribute on an XML element by setting the TextField property of a MenuItemBinding object. 您也可以系結至靜態值。You can also bind to a static value. 如果您設定 Text 物件的屬性 MenuItemBinding ,則套用該物件的所有功能表項目 MenuItemBinding 都會共用相同的靜態文字值。If you set the Text property of a MenuItemBinding object, all menu items to which the MenuItemBinding object is applied share the same static text value. 如需將物件的屬性系結至值的詳細資訊 MenuItem ,請參閱 MenuItemBindingFor more information on binding the properties of a MenuItem object to a value, see MenuItemBinding.

雖然可透過程式設計 DataBindings 方式填入集合,但通常會以宣告方式設定。Although the DataBindings collection can be programmatically populated, it is usually set declaratively. 若要指定功能表項目系結,請先在 <DataBindings> 控制項的開頭和結束記號之間,嵌套開頭和結束記號 MenuTo specify the menu item bindings, first nest opening and closing <DataBindings> tags between the opening and closing tags of the Menu control. 接下來,在 <asp:MenuItemBinding> <DataBindings> 您想要指定的每個功能表項目系結的開頭和結束記號之間放置元素。Next, place <asp:MenuItemBinding> elements between the opening and closing <DataBindings> tags for each menu item binding you want to specify.

適用於

另請參閱