System.Web.UI.WebControls.MenuItemBinding Class

Defines the relationship between a data item and the menu item it is binding to in a System.Web.UI.WebControls.Menu control. This class cannot be inherited.

See Also: MenuItemBinding Members

Syntax

[System.ComponentModel.DefaultProperty("TextField")]
public sealed class MenuItemBinding : ICloneable, System.Web.UI.IDataSourceViewSchemaAccessor, System.Web.UI.IStateManager

Remarks

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

Note:

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

The System.Web.UI.WebControls.Menu control stores its System.Web.UI.WebControls.MenuItemBinding objects in the Menu.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 System.Web.UI.WebControls.Menu control attempts to match the data item to a System.Web.UI.WebControls.MenuItemBinding object to create the corresponding System.Web.UI.WebControls.MenuItem object.

When creating a System.Web.UI.WebControls.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 MenuItemBinding.Depth, the MenuItemBinding.DataMember, or both.

A menu item depth specifies the menu level that gets bound. For example, the following System.Web.UI.WebControls.MenuItemBinding declaration binds the Name and ID fields of the data source to the MenuItem.Text and MenuItem.Value properties, respectively, of all nodes with a depth of 0:

Example

<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 System.Web.UI.IHierarchyData object) exposes a System.Web.UI.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. When a data source contains multiple data item types, the data member specifies which data item type to use. The following System.Web.UI.WebControls.MenuItemBinding declaration binds the <Book> elements of an System.Web.UI.WebControls.XmlDataSource control to all the menu items in the menu, regardless of the location in the hierarchy:

Example

<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 System.Web.UI.WebControls.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 System.Web.UI.WebControls.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 System.Web.UI.WebControls.MenuItem objects to which the System.Web.UI.WebControls.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 System.Web.UI.WebControls.MenuItem object by setting the corresponding property directly.

The following table lists the properties of the System.Web.UI.WebControls.MenuItemBinding class that allow you to bind a property of the System.Web.UI.WebControls.MenuItem object to a field of a data item.

MenuItemBinding.ImageUrlField

The field to bind to the MenuItem.ImageUrl property of a System.Web.UI.WebControls.MenuItem object.

MenuItemBinding.NavigateUrlField

The field to bind to the MenuItem.NavigateUrl property of a System.Web.UI.WebControls.MenuItem object.

MenuItemBinding.TextField

The field to bind to the MenuItem.Text property of a System.Web.UI.WebControls.MenuItem object.

MenuItemBinding.ToolTipField

The field to bind to the MenuItem.ToolTip property of a System.Web.UI.WebControls.MenuItem object.

MenuItemBinding.ValueField

The field to bind to the MenuItem.Value property of a System.Web.UI.WebControls.MenuItem object.

The following table lists the properties of the System.Web.UI.WebControls.MenuItemBinding class that allow you to bind a property of the System.Web.UI.WebControls.MenuItem object to a static value.

MenuItemBinding.ImageUrl

The static value to bind to the MenuItem.ImageUrl property of a System.Web.UI.WebControls.MenuItem object.

MenuItemBinding.NavigateUrl

The static value to bind to the MenuItem.NavigateUrl property of a System.Web.UI.WebControls.MenuItem object.

MenuItemBinding.Target

The static value to bind to the MenuItem.Target property of a System.Web.UI.WebControls.MenuItem object.

MenuItemBinding.Text

The static value to bind to the MenuItem.Text property of a System.Web.UI.WebControls.MenuItem object.

MenuItemBinding.ToolTip

The static value to bind to the MenuItem.ToolTip property of a System.Web.UI.WebControls.MenuItem object.

MenuItemBinding.Value

The static value to bind to the MenuItem.Value property of a System.Web.UI.WebControls.MenuItem object.

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

[The 'ordered' type of list has not been implemented in the ECMA stylesheet.]

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

Requirements

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in System.Web.dll)
Assembly Versions: 2.0.0.0
Since: .NET 2.0