How to set css class on active menu item using a masterpage?

前端 未结 2 1053
鱼传尺愫
鱼传尺愫 2021-01-26 04:20

How to set the Active menu item color when i click the sub submenu items or main item in asp.net master page.

My code is (master page source)

2条回答
  •  攒了一身酷
    2021-01-26 05:03

    Here is what I do. There could be a better way. You can try it.

    NOTE: your link should have runat="server" so you can access them from code-behind

    protected void Page_Load(object sender, EventArgs e)
    {            
        string curlink = Request.RawUrl;
    
        if (curlink.Contains("/administration/school")) 
        {
            schoolinfolink.Attributes["class"] = "selected";
        }
        else if (curlink.Contains("/administration/result"))
        {
            resultlink.Attributes["class"] = "selected";
        }
        else if (curlink.Contains("/administration/staff"))
        {
           staffslink.Attributes["class"] = "selected";
        }
    
    }
    

提交回复
热议问题