MenuEventHandler 代理人

定義

代表處理 MenuItemClick 控制項之 MenuItemDataBound 事件或 Menu 事件的方法。

public delegate void MenuEventHandler(System::Object ^ sender, MenuEventArgs ^ e);
public delegate void MenuEventHandler(object sender, MenuEventArgs e);
type MenuEventHandler = delegate of obj * MenuEventArgs -> unit
Public Delegate Sub MenuEventHandler(sender As Object, e As MenuEventArgs)

參數

sender
Object

事件的來源。

e
MenuEventArgs

MenuEventArgs,其中包含事件資料。

範例

下列程式碼範例示範如何使用 MenuEventHandler 委派,以程式設計方式註冊 控制項事件的 Menu 事件處理常式 MenuItemClick 。 若要讓此範例正常運作,您必須將下面的範例網站地圖資料複製到名為 Web.sitemap 的檔案。


<%@ 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)
  {
    
    // Create a new Menu control.
    Menu newMenu = new Menu();
          
    // Set the properties of the Menu control.
    newMenu.ID = "NavigationMenu";
    newMenu.Orientation = Orientation.Vertical;
    newMenu.Target = "_blank";

    // Specify the data source for the menu.
    newMenu.DataSourceID = "MenuSource";

    // Programmatically register the event-handling method
    // for the MenuItemClick event of a Menu control. 
    newMenu.MenuItemClick += new MenuEventHandler(this.NavigationMenu_MenuItemClick);

    // Add the Menu control to the Controls collection
    // of the PlaceHolder control.
    MenuPlaceHolder.Controls.Add(newMenu);
    
  }

  void NavigationMenu_MenuItemClick(Object sender, MenuEventArgs e)
  {

    // Display the text of the menu item selected by the user.
    Message.Text = "You selected " + e.Item.Text + ".";

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuEventHandler Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuEventHandler Example</h3>
    
      <asp:placeholder id="MenuPlaceHolder"
        runat="server"/>
        
      <asp:sitemapdatasource id="MenuSource"
        runat="server"/>
        
      <hr/>
      
      <asp:label id="Message"
        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)
    
    ' Create a new Menu control.
    Dim newMenu As New Menu()
          
    ' Set the properties of the Menu control.
    newMenu.ID = "NavigationMenu"
    newMenu.Orientation = Orientation.Vertical
    newMenu.Target = "_blank"

    ' Specify the data source for the menu.
    newMenu.DataSourceID = "MenuSource"

    ' Programmatically register the event-handling method
    ' for the MenuItemClick event of a Menu control. 
    AddHandler newMenu.MenuItemClick, AddressOf NavigationMenu_MenuItemClick

    ' Add the Menu control to the Controls collection
    ' of the PlaceHolder control.
    MenuPlaceHolder.Controls.Add(newMenu)
    
  End Sub

  Sub NavigationMenu_MenuItemClick(ByVal sender As Object, ByVal e As MenuEventArgs)

    ' Display the text of the menu item selected by the user.
    Message.Text = "You selected " & e.Item.Text & "."

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuEventHandler Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuEventHandler Example</h3>
    
      <asp:placeholder id="MenuPlaceHolder"
        runat="server"/>
        
      <asp:sitemapdatasource id="MenuSource"
        runat="server"/>
        
      <hr/>
      
      <asp:label id="Message"
        runat="server"/>  

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

以下是上一個範例的網站地圖資料範例。

<siteMap>

<siteMapNode title="Home"

description="Home">

<siteMapNode title="Music"

description="Music">

<siteMapNode title="Classical"

description="Classical"/>

<siteMapNode title="Rock"

description="Rock"/>

<siteMapNode title="Jazz"

description="Jazz"/>

</siteMapNode>

<siteMapNode title="Movies"

description="Movies">

<siteMapNode title="Action"

description="Action"/>

<siteMapNode title="Drama"

description="Drama"/>

<siteMapNode title="Musical"

description="Musical"/>

</siteMapNode>

</siteMapNode>

</siteMap>

備註

類別 MenuEventHandler 是用來表示處理下表中事件的方法。

Event - 事件 描述
MenuItemClick 發生于按一下功能表項目時。 此事件通常用來同步 Menu 處理控制項與頁面上的另一個控制項。
MenuItemDataBound 當功能表項目系結至資料時發生。 這個事件通常用來在控制項中 Menu 轉譯功能表項目之前修改。

建立 MenuEventHandler 委派時,必須識別處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理常式委派的詳細資訊,請參閱 處理和引發事件

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於

另請參閱