问题描述
如何设置从codebehind一个ASP.NET菜单控件的每个菜单项不同的CssClass?
所有这些菜单项都处于2级。因此,像这样是行不通的,因为所有的2级的菜单项会以相同的CssClass(level2class)结束了,我希望能够设置diferent风格为他们每个人的:
How can I set a different cssclass for each menuitem of a ASP.NET Menu control from codebehind?All of these menuitems are at level2. So something like this would not work because all of the menuitems on level2 would end up with the same cssclass(level2class) and I want to be able to set diferent styles for each of them:
<LevelMenuItemStyles>
<asp:MenuItemStyle CssClass="level1class"/>
<asp:MenuItemStyle CssClass="level2class"/>
</LevelMenuItemStyles>
所以我喜欢做这样的事情:
So I'd like to do something like this:
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);
}
另一个受审的替代方案,可以工作,但我也STUCK:
ANOTHER TRIED ALTERNATIVE THAT COULD WORK BUT AM STUCK ALSO:
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?
}
感谢您
推荐答案
但是,如果你真的想要一个CSS类,你可能能够使用Attributes.Add方法......像这样 myItem .Attributes.Add(类,pretty);
But if you really want to a CSS class you might be able to use the Attributes.Add method... something like this myItem.Attributes.Add( "class", "pretty" );
这篇关于ASP.NET:如何设置的CssClass上codebehind一个菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!