共用方式為


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 

事件類型

範例

下列程式碼範例示範如何在控制項中 Menu 顯示功能表項目之前,為修改功能表項目文字 Home 的事件建立事件處理常式 MenuItemDataBound


<%@ 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>

備註

當功能表項目系結至 控制項中的資料 Menu 時,就會 MenuItemDataBound 引發 事件。 這可讓您提供執行自訂常式的事件處理常式,例如在轉譯自訂內容之前新增或修改功能表項目,每當發生此事件時。

MenuEventArgs物件會傳遞至事件處理常式,可讓您存取引發事件的功能表項目屬性。

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱