Menu.MenuItemDataBound 事件

定义

Menu 控件中的菜单项绑定到数据时发生。

public:
 event System::Web::UI::WebControls::MenuEventHandler ^ MenuItemDataBound;
public event System.Web.UI.WebControls.MenuEventHandler MenuItemDataBound;
member this.MenuItemDataBound : System.Web.UI.WebControls.MenuEventHandler 
Public Custom Event MenuItemDataBound As MenuEventHandler 

事件类型

示例

下面的代码示例演示如何为事件创建事件处理程序,MenuItemDataBound该事件处理程序在菜单项显示在控件中Menu之前修改菜单项的文本Home


<%@ 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 NavigationMenu_MenuItemDataBound(Object sender, MenuEventArgs e)
  {
    // Modify the text of the Home menu item by 
    // adding parenthesis around the text. 
    if (e.Item.Text == "Home")
    {
      e.Item.Text = "(" + e.Item.Text + ")";
    }
  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>Menu MenuItemDataBound Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>Menu MenuItemDataBound Example</h3>
    
      <asp:menu id="NavigationMenu"
        staticdisplaylevels="2"
        staticsubmenuindent="10" 
        orientation="Vertical"
        target="_blank"
        datasourceid="MenuSource"
        onmenuitemdatabound="NavigationMenu_MenuItemDataBound"    
        runat="server">

      </asp:menu>
      
      <asp:SiteMapDataSource id="MenuSource"
        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 NavigationMenu_MenuItemDataBound(ByVal sender As Object, ByVal e As MenuEventArgs)
  
    ' Modify the text of the Home menu item by 
    ' adding parenthesis around the text. 
    If e.Item.Text = "Home" Then
    
      e.Item.Text = "(" & e.Item.Text & ")"
    
    End If
      
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>Menu MenuItemDataBound Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>Menu MenuItemDataBound Example</h3>
    
      <asp:menu id="NavigationMenu"
        staticdisplaylevels="2"
        staticsubmenuindent="10" 
        orientation="Vertical"
        target="_blank"
        datasourceid="MenuSource"
        onmenuitemdatabound="NavigationMenu_MenuItemDataBound"    
        runat="server">

      </asp:menu>
      
      <asp:SiteMapDataSource id="MenuSource"
        runat="server"/>        

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

注解

MenuItemDataBound 菜单项绑定到控件中的数据 Menu 时,将引发 事件。 这允许你提供一个事件处理程序,用于执行自定义例程,例如,每当发生此事件时,添加自定义内容或修改菜单项。

对象 MenuEventArgs 将传递给事件处理程序,这使你可以访问引发事件的菜单项的属性。

有关如何处理事件的详细信息,请参阅 处理和引发事件

适用于

另请参阅