MenuItemBindingCollection.Add(MenuItemBinding) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将指定的 MenuItemBinding 对象追加到集合末尾。
public:
int Add(System::Web::UI::WebControls::MenuItemBinding ^ binding);
public int Add (System.Web.UI.WebControls.MenuItemBinding binding);
member this.Add : System.Web.UI.WebControls.MenuItemBinding -> int
Public Function Add (binding As MenuItemBinding) As Integer
参数
- binding
- MenuItemBinding
要追加到集合的结尾的 MenuItemBinding。
返回
插入到集合中的 MenuItemBinding 的索引。
示例
下面的代码示例演示如何使用Add该方法将对象追加MenuItemBinding到控件集合Menu的DataBindings末尾。 若要使此示例正常工作,必须将下面的示例 XML 数据复制到名为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">
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
// Create the menu item bindings for the Menu control.
MenuItemBinding binding;
binding = CreateMenuItemBinding("MapHomeNode", 0, "title", "url");
NavigationMenu.DataBindings.Add(binding);
binding = CreateMenuItemBinding("MapNode", 1, "title", "url");
NavigationMenu.DataBindings.Add(binding);
binding = CreateMenuItemBinding("MapNode", 2, "title", "url");
NavigationMenu.DataBindings.Add(binding);
}
}
// This is a helper method to create a MenuItemBinding
// object from the specified parameters.
MenuItemBinding CreateMenuItemBinding(String dataMember, int depth, String textField, String navigateUrlField)
{
// Create a new MenuItemBinding object.
MenuItemBinding binding = new MenuItemBinding();
// Set the properties of the MenuItemBinding object.
binding.DataMember = dataMember;
binding.Depth = depth;
binding.TextField = textField;
binding.NavigateUrlField = navigateUrlField;
return binding;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>MenuItemBindingCollection Add Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>MenuItemBindingCollection Add Example</h3>
<asp:menu id="NavigationMenu"
staticdisplaylevels="2"
staticsubmenuindent="10"
orientation="Vertical"
target="_blank"
datasourceid="MenuSource"
runat="server">
</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">
<script runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
' Create the menu item bindings for the Menu control.
Dim binding As MenuItemBinding
binding = CreateMenuItemBinding("MapHomeNode", 0, "title", "url")
NavigationMenu.DataBindings.Add(binding)
binding = CreateMenuItemBinding("MapNode", 1, "title", "url")
NavigationMenu.DataBindings.Add(binding)
binding = CreateMenuItemBinding("MapNode", 2, "title", "url")
NavigationMenu.DataBindings.Add(binding)
End If
End Sub
' This is a helper method to create a MenuItemBinding
' object from the specified parameters.
Function CreateMenuItemBinding(ByVal dataMember As String, ByVal depth As Integer, ByVal textField As String, ByVal navigateUrlField As String) As MenuItemBinding
' Create a new MenuItemBinding object.
Dim binding As New MenuItemBinding()
' Set the properties of the MenuItemBinding object.
binding.DataMember = dataMember
binding.Depth = depth
binding.TextField = textField
binding.NavigateUrlField = navigateUrlField
Return binding
End Function
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>MenuItemBindingCollection Add Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>MenuItemBindingCollection Add Example</h3>
<asp:menu id="NavigationMenu"
staticdisplaylevels="2"
staticsubmenuindent="10"
orientation="Vertical"
target="_blank"
datasourceid="MenuSource"
runat="server">
</asp:menu>
<asp:xmldatasource id="MenuSource"
datafile="Map.xml"
runat="server"/>
</form>
</body>
</html>
下面是上一示例的示例站点地图数据。
<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>
注解
Add使用该方法将指定的MenuItemBinding对象追加到集合的末尾。
备注
可以将 null 值和重复 MenuItemBinding 对象添加到集合。
或者,可以使用该方法将对象插入 MenuItemBinding 到集合中的特定索引 Insert 处。
备注
对象在集合中显示的顺序 MenuItemBinding 不会影响这些对象如何应用于控件中的 Menu 菜单项。