Rediger

Del via


Menu.DataBindings Property

Definition

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

Property Value

A MenuItemBindingCollection that represents the relationship between a data item and the menu item it is binding to.

Attributes

Examples

The 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. 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>  

Remarks

The DataBindings collection contains MenuItemBinding objects that define the relationship between a data item and the menu item it is binding to. 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. 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. 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. For example, the data member for an XML element specifies the name of the element.

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. The MenuItemBinding object that defines both a depth and a data member.

  2. The MenuItemBinding object that defines only the depth.

  3. The MenuItemBinding object that defines only the data member.

  4. The MenuItemBinding object that defines neither the depth nor the data member.

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. For 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. 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. For more information on binding the properties of a MenuItem object to a value, see MenuItemBinding.

Although the DataBindings collection can be programmatically populated, it is usually set declaratively. To specify the menu item bindings, first nest opening and closing <DataBindings> tags between the opening and closing tags of the Menu control. Next, place <asp:MenuItemBinding> elements between the opening and closing <DataBindings> tags for each menu item binding you want to specify.

Applies to

See also