Bewerken

Share via


MenuItemBinding Class

Definition

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
Inheritance
MenuItemBinding
Implements

Examples

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

Remarks

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. 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. A MenuItemBinding object defines the relationship between each data item and the menu item it is binding to.

Note

When the Menu control is bound to a SiteMapDataSource control, menu item bindings have no effect. Binding is performed automatically using the site map provider.

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

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, the DataMember, or both.

A menu item depth specifies the menu level that gets bound. 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. 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. 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. 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 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.

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

Note

You can selectively override a bound property in a MenuItem object by setting the corresponding property directly.

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 The field to bind to the ImageUrl property of a MenuItem object.
NavigateUrlField The field to bind to the NavigateUrl property of a MenuItem object.
TextField The field to bind to the Text property of a MenuItem object.
ToolTipField The field to bind to the ToolTip property of a MenuItem object.
ValueField The field to bind to the Value property of a MenuItem object.

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 The static value to bind to the ImageUrl property of a MenuItem object.
NavigateUrl The static value to bind to the NavigateUrl property of a MenuItem object.
Target The static value to bind to the Target property of a MenuItem object.
Text The static value to bind to the Text property of a MenuItem object.
ToolTip The static value to bind to the ToolTip property of a MenuItem object.
Value The static value to bind to the Value property of a MenuItem object.

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.

The MenuItemBinding class also allows you to format the text displayed in a menu item by setting the FormatString property.

Constructors

MenuItemBinding()

Initializes a new instance of the MenuItemBinding class.

Properties

DataMember

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

Depth

Gets or sets the menu depth to which the MenuItemBinding object is applied.

Enabled

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

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

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

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

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

Gets or sets the URL to link to when a menu item to which the MenuItemBinding object is applied is clicked.

NavigateUrlField

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

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

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

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

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

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

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

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

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

Gets or sets the text displayed for the menu item to which the MenuItemBinding object is applied.

TextField

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

Gets or sets the ToolTip text for a menu item to which the MenuItemBinding object is applied.

ToolTipField

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

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

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.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

Explicit Interface Implementations

ICloneable.Clone()

Creates a copy of the MenuItemBinding object.

IDataSourceViewSchemaAccessor.DataSourceViewSchema

For a description of this member, see DataSourceViewSchema.

IStateManager.IsTrackingViewState

Gets a value that indicates whether the MenuItemBinding object is saving changes to its view state.

IStateManager.LoadViewState(Object)

Loads the node's previously saved view state.

IStateManager.SaveViewState()

Saves the view state changes to an Object.

IStateManager.TrackViewState()

Instructs the MenuItemBinding object to track changes to its view state.

Applies to

See also