如何从代码隐藏中为 ASP.NET 菜单控件的每个菜单项设置不同的 cssclass?所有这些菜单项都在 2 级。所以这样的事情是行不通的,因为 level2 上的所有菜单项都会以相同的 cssclass(level2class) 结束,我希望能够为每个菜单项设置不同的样式:
<LevelMenuItemStyles>
<asp:MenuItemStyle CssClass="level1class"/>
<asp:MenuItemStyle CssClass="level2class"/>
</LevelMenuItemStyles>
所以我想做这样的事情:
MenuItem myMenu = new MenuItem();
myMenu.Text = "MyMenu";
Menu1.Items.Add(myMenu); //Menu1 is the name of the asp.net Menu control
foreach (DataRow row in myDataTable.Rows)
{
MenuItem myItem = new MenuItem();
string myItemName = row["myItemName"].ToString();
myItem.Text = myItemName;
//HERE COMES THE PROBLEM. I'M NOT ABLE TO DEFINE A DIFFERENT CSSCLASS FOR EACH MENUITEM.
if (myItemName == "x1")
{
myItem.CssClass = "x1class"; //THIS DOES NOT WORK AS MENUITEM DOES NOT HAVE CSSCLASS PROPERTY
}
else if (myItemName == "x2")
{
myItem.CssClass = "x2class"; //THIS DOES NOT WORK AS MENUITEM DOES NOT HAVE CSSCLASS PROPERTY
}
myMenu.ChildItems.Add(myItem);
}
另一个尝试过的替代方案,它可以工作,但我也卡住了:
if (myItemName == "x1")
{
MenuItemStyle ms = new MenuItemStyle();
ms.CssClass = "x1class";
//HOW DO I SET ms OBJECT TO THE myItem OBJECT?
}
else if (myItemName == "x2")
{
MenuItemStyle ms = new MenuItemStyle();
ms.CssClass = "x2class";
//HOW DO I SET ms OBJECT TO THE myItem OBJECT?
}
谢谢