Here is some sample code that works and should give you an idea of how to acheive what you want.
We have an Aspx page with a menu called MainMenu. We set two styles on it for menu items and selected menu items.
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestWebApplication._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<link href="Stylesheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Menu ID="MainMenu"
runat="server"
StaticSelectedStyle-CssClass="selected_menu_item"
StaticMenuItemStyle-CssClass"menu_item" />
</div>
</form>
</body>
</html>
On page load in the code behind we build our menu and store an Identifer for each page in the menu item values. We then compare those item values with the name of the page we are currently on to work out which menu item we want to look differently. We then set the selected property on that menu item.
Code:
using System;
using System.Web.UI.WebControls;
namespace TestWebApplication
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Create menu items as store the path/name for each
MainMenu.Items.Add(new MenuItem("Page 1", "~/Page1.aspx"));
MainMenu.Items.Add(new MenuItem("Page 2", "~/Page2.aspx"));
MainMenu.Items.Add(new MenuItem("Page 3", "~/Page3.aspx"));
//Work out what page we are viewing
string pageViewed = this.Page.AppRelativeVirtualPath;
//Go through each menu item and work out which one we want to
//select
foreach (MenuItem item in MainMenu.Items)
{
if (pageViewed == item.Value)
{
item.Selected = true;
}
}
}
}
}
Finally, we need a stylesheet.
Code:
.menu_item
{
font-weight: normal;
}
.selected_menu_item
{
font-weight: bold;
}
In this example, I have stored the actual page names but I normally use an enumerator and a lookup to a list of page names to make it easier to decouple the naming of the actual pages with the code for setting the menu items.
Bookmarks