Search content within the blog

Thursday, November 20, 2008

Highlighting the selected item in the menu

Note : This wont work if an update panel is present.

Master Pasge contents having menu

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MenuItemTest.master.cs"
Inherits="Master_Pages_MenuItemTest" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Menu ID="NavigationMenu" StaticDisplayLevels="1" StaticSubMenuIndent="1" Orientation="horizontal"
Target="_blank" runat="server" OnMenuItemClick="NavigationMenu_MenuItemClick">
<StaticSelectedStyle BackColor="Black" Font-Italic="true" ForeColor="Black" BorderColor="Red" />
<Items>
<asp:MenuItem NavigateUrl="~/Menus/Menu2.aspx" Text="Menu2" Target="_Self"></asp:MenuItem>
<asp:MenuItem NavigateUrl="~/Menus/Menu1.aspx" Text="Menu1" Target="_Self"></asp:MenuItem>
</Items>
</asp:Menu>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>

Add two pages namely menu1.aspx, menu2.aspx

Menu1.aspx
<%@ Page Language="C#" MasterPageFile="~/Master Pages/MenuItemTest.master" AutoEventWireup="true" CodeFile="Menu1.aspx.cs" Inherits="Menus_Menu1" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>

Menu1.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
Menu M = (Menu)this.Master.FindControl("NavigationMenu");
if (M != null)
{
M.Items[1].Selected = true;
}
}



Menu2.aspx
<%@ Page Language="C#" MasterPageFile="~/Master Pages/MenuItemTest.master" AutoEventWireup="true" CodeFile="Menu2.aspx.cs" Inherits="Menus_Menu2" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>



Menu2.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
Menu M = (Menu)this.Master.FindControl("NavigationMenu");
if (M != null)
{
M.Items[0].Selected = true;
}
}

No comments:

Post a Comment