简体   繁体   English

如何在MenuItemDataBound中的MenuItem上设置CSS

[英]How set CSS on MenuItem in MenuItemDataBound

I've been trying to find a way to style the asp.menu control for a while. 我一直试图寻找一种样式化asp.menu控件的方法。 Many of the examples online were not helpful as the attributes for setting styles does not work (ie StaticMenuItemStyle-CssClass="SOMECLASS"). 网上的许多示例都无济于事,因为设置样式的属性不起作用(即StaticMenuItemStyle-CssClass =“ SOMECLASS”)。 So I was hoping there was a way to do it programmatically? 所以我希望有一种方法可以通过编程来实现? Please help. 请帮忙。

protected void Menu_MenuItemDataBound(object sender, MenuEventArgs e)
{
    if (e.Item.NavigateUrl.Trim() == _currentUrl.Trim())
    {
        // Something like this
        e.Item.CssClass = "SOMECLASS";
    }
}

EDIT I've done a work around for my Site Navigation. 编辑我已经完成了网站导航的工作。 If there is a way to use ASP.NET Menu Style Attributes let me know! 如果有使用ASP.NET菜单样式属性的方法,请告诉我!

protected void Menu_MenuItemDataBound(object sender, MenuEventArgs e)
{
    MenuItem menuitem = (MenuItem)e.Item;
    if (menuitem.NavigateUrl.Trim() == _currentUrl.Trim())
    {
        if (menuitem.Depth == 1)
        {
            menuitem.Text = "<span class=\"active" + menuitem.Depth + " selectedlevel1\">" + menuitem.Text + "</span>";
        }
        else
        {
            menuitem.Text = "<span class=\"active" + menuitem.Depth + " selectedlevel2\">" + menuitem.Text + "</span>";
        }
        while (menuitem.Parent != null)
        {
            menuitem = menuitem.Parent;
            String title = menuitem.Text;
            title = title.Replace("<span>", "");
            title = title.Replace("</span>", "");
            menuitem.Text = "<span class=\"active" + menuitem.Depth + "\">" + title + "</span>";
        }
    }
    else
    {
        menuitem.Text = "<span>" + menuitem.Text + "</span>";
    }
}

You should mark the item as selected 您应该将项目标记为选中

e.Item.Selected = true;

And set DynamicSelectedStyle-CssClass (and / or StaticSelectedStyle-CssClass , honestly not sure which one applies on your case) with "SOMECLASS" 并使用"SOMECLASS"设置DynamicSelectedStyle-CssClass (和/或StaticSelectedStyle-CssClassStaticSelectedStyle-CssClass不确定哪个适用于您的情况)。

EDIT 编辑

It seems that there is a problem Menu control and selected class. 似乎Menu控件和选定的类存在问题。 This other question presents a workaround for your scenario that might help you. 这个其他问题为您的方案提供了一种解决方法,可能会对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM