ASP.NET菜单项个人风格 [英] ASP.NET MenuItem Individual Styles

查看:134
本文介绍了ASP.NET菜单项个人风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望通过我的站点使用一个ASP.NET菜单控制导航。不过,我有每个菜单项必须以不同风格的要求(不同的颜色,静态和onHover)。如果没有创建将从菜单项继承的自定义类,这可能吗?

I'm hoping to use an ASP.NET Menu Control for navigation through my site. However, I've got a requirement that each MenuItem must be styled differently (different colors, both static, and onHover). Without creating a custom class that would inherit from MenuItem, is this possible?

这是一个更好的解决方案的想法?

Thoughts on a better solution?

推荐答案

在菜单覆盖RenderContents短,你的选择是非常有限的。大部分你所需要的是私人和密封,你不会得到任何有。

Short of overriding RenderContents on Menu, your options are very limited. Most of what you'd need is private and sealed and you won't get anywhere there.

我的解决办法是使用模板。你可以使用MenuItem.Value或深度和和的ItemIndex来识别每个项目,并提供必要的属性。

My solution would be to use templates. You could use MenuItem.Value or Depth and and ItemIndex to identify each item and provide necessary attributes.

在页:

<asp:Menu ID="menu" runat="server" DynamicHorizontalOffset="2" StaticSubMenuIndent="10px">
	<Items>
		<asp:MenuItem Text="Item 1" Value="value 1">
			<asp:MenuItem Text="Item 2" Value="value 2">
				<asp:MenuItem Text="Item 3" Value="value 3"></asp:MenuItem>
			</asp:MenuItem>
			<asp:MenuItem Text="Item 4" Value="value 4">
				<asp:MenuItem Text="Item 5" Value="value 5"></asp:MenuItem>
			</asp:MenuItem>
			<asp:MenuItem Text="Item 6" Value="value 6"></asp:MenuItem>
		</asp:MenuItem>
		<asp:MenuItem Text="Item 7" Value="value 7"></asp:MenuItem>
		<asp:MenuItem Text="Item 8" Value="value 8"></asp:MenuItem>
	</Items>
	<StaticItemTemplate>
		<asp:Panel runat="server" ForeColor='<%# GetItemColor(Container) %>'>
			<%# Eval("Text") %> - <%# Eval("Value") %>
		</asp:Panel>
	</StaticItemTemplate>
	<DynamicItemTemplate>
		<asp:Panel ID="Panel1" runat="server" ForeColor='<%# GetItemColor(Container) %>'>
			<%# Eval("Text") %> - <%# Eval("Value") %>
		</asp:Panel>
	</DynamicItemTemplate>
</asp:Menu>

在code(不去管这code的愚蠢,它只是证明原则):

In Code (never mind silliness of this code, it is just to demonstrate the principle):

public Color GetItemColor(MenuItemTemplateContainer container)
{
	MenuItem item = (MenuItem)container.DataItem;

	//identify based value
	if (item.Value == "value 2")
		return Color.Brown;

	//identify based on depth and index
	if (item.Depth == 0)
		switch (container.ItemIndex)
		{
			case 0: return Color.Red;
			case 1: return Color.Blue;
			case 2: return Color.DarkGreen;
			default:
				return Color.Black;
		}
	else
		switch (container.ItemIndex)
		{
			case 0: return Color.Purple;
			case 1: return Color.Aqua;
			case 2: return Color.DarkOrange;
			default:
				return Color.Black;
		}
}

这篇关于ASP.NET菜单项个人风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆