Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET 2.0 Beta Preview - B. Evjen.pdf
Скачиваний:
26
Добавлен:
24.05.2014
Размер:
15.33 Mб
Скачать

Site Navigation

Menu Events

The Menu control exposes events such as

DataBinding

DataBound

Disposed

Init

Load

MenuItemClick

MenuItemDataBound

PreRender

Unload

One nice event to be aware of is the MenuItemClick event. This event, shown in Listing 5-23, allows you to take some action when the end user clicks one of the available menu items.

Listing 5-23: Using the MenuItemClick event

VB

Sub Menu1_MenuItemClick(ByVal sender As Object, _

ByVal e As System.Web.UI.WebControls.MenuEventArgs)

‘ Code for event here

End Sub

C#

void Menu1_MenuItemClick(object sender, MenuEventArgs e)

{

// Code for event here

}

This event uses the MenuEventArgs event delegate and provides you access to the text and value of the item selected from the menu.

Binding the Menu control to an XML file

Just as with the TreeView control, it is possible to bind the Menu control to items that come from other data source controls provided with ASP.NET 2.0. Although most developers are likely to use the Menu control to enable end users to navigate to URL destinations, you can also use the Menu control to enable users to make selections.

As an example, take the previous XML file, Hardware.xml, which was used with the TreeView control from Listing 5-8 earlier in the chapter. For this example,the Menu control needs to work with an

163

Chapter 5

XmlDataSource control. When the end user makes a selection from the menu, you populate a Listbox on the page with the items selected. The code for this is shown in Listing 5-24.

Listing 5-24: Using the Menu control with an XML file

VB

<%@ Page Language=”VB” %>

<script runat=”server”>

Sub Menu1_MenuItemClick(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.MenuEventArgs)

Listbox1.Items.Add(e.Item.Parent.Value & “ : “ & e.Item.Value) End Sub

</script>

<html xmlns=”http://www.w3.org/1999/xhtml” > <head runat=”server”>

<title>Menu Server Control</title> </head>

<body>

<form id=”form1” runat=”server”>

<asp:Menu ID=”Menu1” Runat=”server” DataSourceID=”XmlDataSource1” OnMenuItemClick=”Menu1_MenuItemClick”>

<Bindings>

<asp:MenuItemBinding DataMember=”Item” TextField=”Category”></asp:MenuItemBinding> <asp:MenuItemBinding DataMember=”Option” TextField=”Choice”></asp:MenuItemBinding>

</Bindings>

</asp:Menu>

<p>

<asp:ListBox ID=”Listbox1” Runat=”server”> </asp:ListBox></p>

<asp:xmldatasource ID=”XmlDataSource1” runat=”server” datafile=”Hardware.xml” />

</form>

</body>

</html>

C#

<%@ Page Language=”C#” %>

<script runat=”server”>

void Menu1_MenuItemClick(object sender, MenuEventArgs e)

{

Listbox1.Items.Add(e.Item.Parent.Value + “ : “ + e.Item.Value);

}

</script>

From this example, you can see that instead of using the <asp:TreeNodeBinding> elements, as I did with the TreeView control, the Menu control uses the <asp:MenuItemBinding> elements to make connections to items listed in the XML file: Hardware.xml. In addition, the root element of the Menu control, the <asp:Menu> element, now includes the OnMenuItemClick attribute, which points to the event delegate Menu1_MenuItemClick.

164