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");


                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;

            TableCell tc2 = new TableCell();
            tc2.Width = Unit.Percentage(100);
            tc2.CssClass = "menu";



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.


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";
    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.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.