0

I have this code in C# code-behind:

for (int i = PIndex2; i < PIndex2 + 10 && i < PCount; i++)
{
    NavMenu.Items.Add(new MenuItem
    {
        Text = (i + 1).ToString(),
        NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (i + 1).ToString()
    });
    if (i == PIndex)
    {
        MenuItemStyle ms = new MenuItemStyle();
        ms.CssClass = "div.navmenu ul li a:focus";
    }
}

I want to change the background color if the page is the current page (which I keep in a variable called PIndex). I can't seem to find the proper syntax to do it. I have tried using both a:focus and a:active in my CssClass statement, neither of which worked.

My CSS is as follows:

/* NAV MENU */
div.navmenu
{
    padding: 2px 0px 2px 4px;
    display: table;
    margin: 0 auto;
    text-align: center;
}

div.navmenu ul
{
    list-style: none;
    margin: 0px;
    padding: 0px;
    width: auto;
    color: White;
}

div.navmenu ul li a, div.navmenu ul li a:visited
{
    background-color: #465c71;    
/*    background-color: white;    */
    border: 1px #4e667d solid;  
    color: white;
    display: block;
    line-height: 1.35em;
    padding: 4px 20px;
    text-decoration: none;
    white-space: nowrap;
}

div.navmenu ul li a:hover
{
    background-color: white;
    color: #465c71;
    text-decoration: none;
}

div.navmenu ul li a:active, div.navmenu ul li a:focus,
{
    background-color: #cfdbe6 !important;
    /*    color: #cfdbe6;  */
    color: White; 
    text-decoration: none;
}

Hover works fine. However, once I'm on the page I want the item's background color to be what's in a:active.

Johnny Bones
  • 8,786
  • 7
  • 52
  • 117
  • Is this a Windows Forms app? Or a WPF app? – JeffFerguson Jun 30 '15 at 12:42
  • @JeffFerguson - It's ASP.NET Web App. – Rahul Singh Jun 30 '15 at 12:44
  • Define CSS class, add class attribute to menu item. See duplicate. – CodeCaster Jun 30 '15 at 12:48
  • If you read the duplicate, you'd know you can't add a class attribute. And that question isn't a duplicate of this one. – Johnny Bones Jun 30 '15 at 12:53
  • See the ["found out how"](http://stackoverflow.com/questions/19883721/asp-net-how-to-set-the-cssclass-for-a-menuitem-on-codebehind#comment29638465_19884003) comment. Please expand your question on what part specifically you're having trouble. Do you know you can style HTML elements with CSS? – CodeCaster Jun 30 '15 at 12:58
  • I'm using CSS, and it's not working. – Johnny Bones Jun 30 '15 at 13:24
  • What did that code do with `ms` afterwards? Please show all relevant code. You need to apply the style to the menu item. – CodeCaster Jun 30 '15 at 14:34
  • I don't understand the question. The code with ms did nothing. Didn't change the background, and it didn't break anything. The page loaded, but without the colors I expected for that associated page number. – Johnny Bones Jun 30 '15 at 15:19
  • Use "@username" if you want the user to be notified of your comment. Anyhow you need to do something with the ms variable. Show your code, with the MenuItemStyle. – CodeCaster Jun 30 '15 at 18:09
  • @CodeCaster - Question has been edited to show my exact code that isn't working. – Johnny Bones Jun 30 '15 at 18:48
  • @JohnnyBones, try see about [Menu.StaticSelectedStyle Property](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.menu.staticselectedstyle(v=vs.110).aspx) – Grundy Jun 30 '15 at 19:07
  • @Grundy - If you put that as an answer, I'll give ya 25 points. :) Your suggestion led to this question, which (with a bit of tweaking) solved my problem: http://stackoverflow.com/questions/7148872/asp-net-problems-with-static-selected-style-for-a-selected-page-on-the-menu – Johnny Bones Jun 30 '15 at 19:38
  • @JohnnyBones, methinks, better mark this question as duplicate, or you can put your own answer with your tweaking for those who find this question in future :-) – Grundy Jul 01 '15 at 06:23

0 Answers0