Modifier

Partager via


MenuItemBinding.DataMember Property

Definition

Gets or sets the data member to bind to a menu item.

public:
 property System::String ^ DataMember { System::String ^ get(); void set(System::String ^ value); };
public string DataMember { get; set; }
member this.DataMember : string with get, set
Public Property DataMember As String

Property Value

The data member to bind to a menu item. The default is an empty string (""), which indicates that this property is not set.

Examples

The following code example demonstrates how to use the DataMember property to specify which XML element to bind to 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 MenuDataMember.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 DataMember Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>MenuItemBinding DataMember Example</h3>

      <asp:menu id="NavigationMenu"
        datasourceid="MenuSource"
        runat="server">
        
        <databindings>
        
          <asp:menuitembinding datamember="MapNodeHome"
            textfield="Title"
            valuefield="Description"
            imageurlfield="ImageUrl"
            tooltipfield="ToolTip"
            target="_self" />
          <asp:menuitembinding datamember="MapNodeCategory"
            textfield="Title"
            valuefield="Description"
            imageurlfield="ImageUrl"
            tooltipfield="ToolTip"
            target="_blank"/>
          <asp:menuitembinding datamember="MapNodeSection"
            textfield="Title"
            valuefield="Description"
            imageurlfield="ImageUrl"
            tooltipfield="ToolTip"
            target="_blank"/>
          
        </databindings>
        
      </asp:menu>

      <asp:xmldatasource id="MenuSource"
        datafile="MenuDataMember.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 DataMember Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>MenuItemBinding DataMember Example</h3>

      <asp:menu id="NavigationMenu"
        datasourceid="MenuSource"
        runat="server">
        
        <databindings>
        
          <asp:menuitembinding datamember="MapNodeHome"
            textfield="Title"
            valuefield="Description"
            imageurlfield="ImageUrl"
            tooltipfield="ToolTip"
            target="_self" />
          <asp:menuitembinding datamember="MapNodeCategory"
            textfield="Title"
            valuefield="Description"
            imageurlfield="ImageUrl"
            tooltipfield="ToolTip"
            target="_blank"/>
          <asp:menuitembinding datamember="MapNodeSection"
            textfield="Title"
            valuefield="Description"
            imageurlfield="ImageUrl"
            tooltipfield="ToolTip"
            target="_blank"/>
          
        </databindings>
        
      </asp:menu>

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

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

The following code is sample site map data for the previous example.

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

Title="Home"

Description="Root Page"

ToolTip="Home Page">

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

Title="Music"

Description="Music Category"

ToolTip="Music Page">

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

Title="Classical"

Description="Classical Section"

ToolTip="Classical Page"/>

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

Title="Rock"

Description="Rock Section"

ToolTip="Rock Page"/>

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

Title="Jazz"

Description="Jazz Section"

ToolTip="Jazz Page"/>

</MapNodeCategory>

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

Title="Movies"

Description="Movies Category"

ToolTip="Movies Page">

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

Title="Action"

Description="Action Section"

ToolTip="Action Page"/>

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

Title="Drama"

Description="Drama Section"

ToolTip="Drama Page"/>

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

Title="Musical"

Description="Musical Section"

ToolTip="Musical Page"/>

</MapNodeCategory>

</MapNodeHome>

Remarks

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. You can specify the Depth property, the DataMember property, or both.

When the data source contains multiple elements or tables, use the DataMember property to specify the element or table to bind to a menu item. 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. Each data item in a hierarchical data source (represented by a System.Web.UI.IHierarchyData object) exposes a IHierarchyData.Type property, which specifies the type of the data item. For example, the data member for an XML element specifies the name of the element. 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. For example, you can have <Item> elements that appear at different levels within an XML file. 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 or 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.

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. 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 field from the data source.

If conflicting MenuItemBinding objects are defined, the Menu control applies the menu item bindings in the following order of precedence:

  1. The MenuItemBinding object that defines and matches both a depth and a data member.

  2. The MenuItemBinding object that defines and matches the data member only.

  3. The MenuItemBinding object that defines and matches the depth only.

  4. The 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. The MenuItemBinding object that does not have a match in the data source. 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.

Applies to

See also