Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

Menu.DataBindings Property

Gets a collection of MenuItemBinding objects that define the relationship between a data item and the menu item it is binding to.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
[PersistenceModeAttribute(PersistenceMode.InnerProperty)]
public MenuItemBindingCollection DataBindings { get; }

Property Value

Type: System.Web.UI.WebControls.MenuItemBindingCollection
A MenuItemBindingCollection that represents the relationship between a data item and the menu item it is binding to.

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.

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>

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>

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.