MenuEventHandler Delegat

Definicja

Reprezentuje metodę MenuItemClick , która obsługuje zdarzenie lub MenuItemDataBound zdarzenie kontrolki 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)

Parametry

sender
Object

Źródło zdarzenia.

e
MenuEventArgs

Element MenuEventArgs zawierający dane zdarzenia.

Przykłady

W poniższym przykładzie kodu pokazano, jak programowo zarejestrować programową procedurę Menu obsługi zdarzeń dla MenuItemClick zdarzenia kontrolki za pomocą MenuEventHandler delegata. Aby ten przykład działał poprawnie, należy skopiować przykładowe dane mapy witryny poniżej do pliku o nazwie 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>

Poniżej przedstawiono przykładowe dane mapy witryny dla poprzedniego przykładu.

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

Uwagi

Klasa MenuEventHandler służy do reprezentowania metody obsługującej zdarzenia w poniższej tabeli.

Zdarzenie Opis
MenuItemClick Występuje po kliknięciu elementu menu. To zdarzenie jest często używane do synchronizowania kontrolki Menu z inną kontrolką na stronie.
MenuItemDataBound Występuje, gdy element menu jest powiązany z danymi. To zdarzenie jest często używane do modyfikowania elementu menu przed jego renderowaniem w kontrolce Menu .

Podczas tworzenia delegata należy zidentyfikować metodę MenuEventHandler , która będzie obsługiwać zdarzenie. Aby skojarzyć zdarzenie z programem obsługi zdarzeń, dodaj wystąpienie delegata do zdarzenia. Program obsługi zdarzeń jest wywoływany przy każdym wystąpieniu zdarzenia, o ile nie usunięto delegata. Aby uzyskać więcej informacji na temat delegatów programu obsługi zdarzeń, zobacz Obsługa i podnoszenie zdarzeń.

Metody rozszerzania

GetMethodInfo(Delegate)

Pobiera obiekt reprezentujący metodę reprezentowaną przez określonego delegata.

Dotyczy

Zobacz też