Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using asp Menu Control. I was binding Menu items from database. However, I've got a requirement that each MenuItem's Background color must be different colors(i.e) Each menu will be displayed in different colur. Is this possible to do that? or Suggest me any other method to do.

Many thanks in advance
Posted
Updated 7-Apr-14 1:53am
v3

 
Share this answer
 
Comments
Archana Venkatramani 7-Apr-14 7:29am    
Hi Nagaraj Muthuchamy,
Thanks for your reply. I have tried that link that you have provided. Its changing the color around the text not full menu.
Nagaraj Muthuchamy 7-Apr-14 7:38am    
If you want to change the background color of the menu item, change 'ForeColor' to 'BackColor' in asp:Panel.
Archana Venkatramani 7-Apr-14 7:52am    
Yes i have changed 'backcolor' from 'ForeColor'.. It is changing the color around the text. Not full menu.
Nagaraj Muthuchamy 7-Apr-14 7:54am    
Please check the following link, this will apply styles for full Menu control.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.menuitemstyle%28v=vs.90%29.aspx
Archana Venkatramani 7-Apr-14 7:57am    
I need to display the menu like the following link.
http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-56-40-metablogapi/2605.4_5F00_0631ECBB.png

But the menu should be taken from database. That's why i am using asp menu control. In that i need to change each and every menu item background color. This background color code also should be taken from database.
<br />
public partial class WebForm1 : System.Web.UI.Page<br />
    {<br />
        protected void Page_Load(object sender, EventArgs e)<br />
        {<br />
<br />
            DataTable dt = new DataTable();<br />
<br />
            dt.Columns.Add("pkid");<br />
            dt.Columns.Add("MenuName");<br />
            dt.Columns.Add("ParentID");<br />
            dt.Columns.Add("MenuLocation");<br />
<br />
            dt.Rows.Add("1", "Parent 1", 0, "#");<br />
            dt.Rows.Add("2", "Parent 2", 0, "#");<br />
<br />
            dt.Rows.Add("3", "Child 1-1", 1, "#");<br />
            dt.Rows.Add("4", "Parent 1-2", 1, "#");<br />
            dt.Rows.Add("5", "Parent 1-3", 1, "#");<br />
<br />
            dt.Rows.Add("6", "Child 2-1", 2, "#");<br />
            dt.Rows.Add("7", "Parent 3-2", 2, "#");<br />
            dt.Rows.Add("8", "Parent 3-3", 2, "#");<br />
<br />
            foreach (DataRow dr in dt.Select("ParentID =" + 0)) <br />
            {<br />
                MenuItem mnu = new MenuItem(dr["MenuName"].ToString(), dr["pkid"].ToString(), "", dr["MenuLocation"].ToString());<br />
                menuBar.Items.Add(mnu); <br />
            }<br />
<br />
            foreach (DataRow dr in dt.Select("ParentID >" + 0)) <br />
            { <br />
                MenuItem mnu = new MenuItem(dr["MenuName"].ToString(), dr["pkid"].ToString(), "", dr["MenuLocation"].ToString()); <br />
                menuBar.FindItem(dr["ParentID"].ToString()).ChildItems.Add(mnu); <br />
            } <br />
        }<br />
<br />
        public Color GetItemColor(MenuItemTemplateContainer container)<br />
        {<br />
            MenuItem item = (MenuItem)container.DataItem;<br />
<br />
            //identify based value<br />
            //if (item.Value == "value 2")<br />
            //    return Color.Brown;<br />
<br />
            //identify based on depth and index<br />
            if (item.Depth == 0)<br />
                switch (Convert.ToInt32(item.Value))<br />
                {<br />
                    case 0: return Color.Red;<br />
                    case 1: return Color.Blue;<br />
                    case 2: return Color.DarkGreen;<br />
                    default:<br />
                        return Color.Black;<br />
                }<br />
<br />
            return Color.Black;<br />
            //else<br />
                //switch (container.ItemIndex)<br />
                //{<br />
                //    //case 0: return Color.Purple;<br />
                //    //case 1: return Color.Aqua;<br />
                //    //case 2: return Color.DarkOrange;<br />
                //    //default:<br />
                //        return Color.Black;<br />
                //}<br />
<br />
<br />
        }<br />
    }<br />


XML
<asp:Menu ID="menuBar" runat="server" Orientation="Horizontal"  StaticPopOutImageUrl="~/Images/down.png">
        <StaticItemTemplate>
        <asp:Panel runat="server" BackColor='<%# GetItemColor(Container) %>' ForeColor="White">
            <%# Eval("Text") %> - <%# Eval("Value") %>
        </asp:Panel>
    </StaticItemTemplate>
    <DynamicItemTemplate>
        <asp:Panel ID="Panel1" runat="server" BackColor='<%# GetItemColor(Container) %>' ForeColor="White">
            <%# Eval("Text") %> - <%# Eval("Value") %>
        </asp:Panel>
    </DynamicItemTemplate>
    </asp:Menu>
 
Share this answer
 
Comments
Archana Venkatramani 7-Apr-14 8:45am    
i haved applied your solution. Now the menu is displaying like this. Please check the below URL.

http://www.dotnetspider.com/attachments/forum/334677-54831-Menu.png

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900