0

Hi friends,
i am buliding my menu in the master pager like below in C# and asp.net 2.0 and iam get these menu from database dynamically

private void GenerateMenuItems()
    {
        ContentManagement.Menu[] menuList = (ContentManagement.Menu[])Session["MenuItems"];
        if (menuList != null)
        {

            Table menuTable = new Table();
            menuTable.Width = Unit.Percentage(100);
            menuTable.BorderStyle = BorderStyle.None;
            menuTable.CellSpacing = 0;
            menuTable.CellPadding = 0;

            TableRow tablewRow = new TableRow();

            foreach (ContentManagement.Menu menu in menuList)
            {
                TableCell tc = new TableCell();
                tc.CssClass = "menu";

                HtmlAnchor a = new HtmlAnchor();
                a.HRef = "~/Student/" + menu.MenuUrl;
                a.InnerHtml = menu.MenuName;
                a.Attributes.Add("class", "menu_lnk");
                a.Attributes.Add("OnClick", "san");
                tc.Controls.Add(a);

                tablewRow.Cells.Add(tc);

                TableCell tc1 = new TableCell();
                tc1.Width = Unit.Pixel(1);

                HtmlImage img = new HtmlImage();
                img.Src = "../App_Themes/images/menu-sep.gif";
                img.Width = 6;
                img.Height = 27;
                tc1.Controls.Add(img);
                tablewRow.Cells.Add(tc1);
            }

            TableCell tc2 = new TableCell();
            tc2.Width = Unit.Percentage(100);
            tc2.CssClass = "menu";
            tablewRow.Cells.Add(tc2);
            menuTable.Rows.Add(tablewRow);

            menuPanel.Controls.Add(menuTable);

        }
    }

now what i want is when some one clicks on any of the links above, i want to change the background color by assigning a css class called selected, so the user will know on what page he/she is on.
any suggestions please.
any ideas how to do it. i appreciate it.

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Airshow
0

Vytla,

I'm not familiar with asp.net so may well have got the syntax wrong, however I'm sure you can fix that.

In principle, this is what you need to do:

First, you must tell the menu builder funciton which menu item needs to be styled as "selected"

private void GenerateMenuItems(selectedMenuName)

Then, you need to style the table cell for the required menu item differently from the others. Inside the foreach loop, replace tc.CssClass = "menu"; with:

if(menu.MenuName == selectedMemuName)
{
    tc.CssClass = "menu";
}
else
{
    tc.CssClass = "menu selected";
}

Of course, this relies on knowing selectedMemuName before GenerateMenuItems is called. If you don't know the name, then maybe you know the index of the selected item instead, and you can modify the code accordingly. Ultimately, you must know something unique in order to identify the menu item that needs to be highlighted.

Airshow

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.