The following code example demonstrates how to use the MenuEventArgs object passed to the event handler for the MenuItemDataBound event to modify the properties of a menu item before it is displayed in a Menu control. The ImageUrl property of the Home menu item is set to display an image in that menu item only. For this example to work correctly, you must copy the sample site map data below to a file named Web.sitemap.
<%@ 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 MenuItemDataBound_NavigationMenu(ByVal sender As Object, ByVal e As MenuEventArgs)
' Display an image for the Home menu item only by
' setting its ImageUrl property.
If e.Item.Text = "Home" Then
e.Item.ImageUrl = "Images\Home.jpg"
End If
End Sub
</script>
<html >
<head runat="server">
<title>MenuEventArgs Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>MenuEventArgs Example</h3>
<asp:menu id="NavigationMenu"
staticdisplaylevels="2"
staticsubmenuindent="10"
orientation="Vertical"
datasourceid="menusource"
onmenuitemdatabound="MenuItemDataBound_NavigationMenu"
runat="server">
</asp:menu>
<asp:SiteMapDataSource id="MenuSource"
Runat="server"/>
</form>
</body>
</html>
<%@ 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 MenuItemDataBound_NavigationMenu(Object sender, MenuEventArgs e)
{
// Display an image for the Home menu item only by
// setting its ImageUrl property.
if (e.Item.Text == "Home")
{
// Use an @-quoted string to bypass the escape sequence
// processing.
e.Item.ImageUrl = @"Images\Home.jpg";
}
}
</script>
<html >
<head runat="server">
<title>MenuEventArgs Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>MenuEventArgs Example</h3>
<asp:menu id="NavigationMenu"
staticdisplaylevels="2"
staticsubmenuindent="10"
orientation="Vertical"
datasourceid="menusource"
onmenuitemdatabound="MenuItemDataBound_NavigationMenu"
runat="server">
</asp:menu>
<asp:SiteMapDataSource id="MenuSource"
Runat="server"/>
</form>
</body>
</html>
The following is sample site map data for the previous example.
<siteMap>
<siteMapNode url="~\Home.aspx"
title="Home"
description="Home">
<siteMapNode url="~\Music.aspx"
title="Music"
description="Music">
<siteMapNode url="~\Classical.aspx"
title="Classical"
description="Classical"/>
<siteMapNode url="~\Rock.aspx"
title="Rock"
description="Rock"/>
<siteMapNode url="~\Jazz.aspx"
title="Jazz"
description="Jazz"/>
</siteMapNode>
<siteMapNode url="~\Movies.aspx"
title="Movies"
description="Movies">
<siteMapNode url="~\Action.aspx"
title="Action"
description="Action"/>
<siteMapNode url="~\Drama.aspx"
title="Drama"
description="Drama"/>
<siteMapNode url="~\Musical.aspx"
title="Musical"
description="Musical"/>
</siteMapNode>
</siteMapNode>
</siteMap>