Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi..

I am using an menu control for which i am adding menuitems programmatically as follows:-

C#
MenuItem mi = new MenuItem(TextBox1.Text);
        Menu1.Items.Add(mi);
        MenuItem mi1 = new MenuItem(TextBox2.Text);
        mi.ChildItems.AddAt(0, mi1);
        MenuItem mi2 = new MenuItem(TextBox3.Text);
        mi1.ChildItems.AddAt(0, mi2);

Menu1
Menu2
Menu3

but if i have to adding another menu for Menu2 or for Menu1 is not possible..

Can anyone please guide me??
Posted
Updated 16-Jan-12 21:06pm
v2
Comments
Anuja Pawar Indore 17-Jan-12 3:07am    
Added pre tag

What's the problem? Try this
C#
MenuItem mi = new MenuItem(TextBox1.Text);
Menu1.Items.Add(mi);
MenuItem mi1 = new MenuItem(TextBox2.Text);
mi.ChildItems.Add(mi1);
mi1 = new MenuItem("Other" + TextBox2.Text);
mi.ChildItems.Add(mi1);
MenuItem mi2 = new MenuItem(TextBox3.Text);
mi1.ChildItems.Add(mi2);
 
Share this answer
 
Comments
bhagyap 17-Jan-12 4:02am    
Thanks for ur reply.. but i dont want it to append by default to other menu items and also i want the state to be retained wen i debug it next time..
Database driven menu is best one. Alternatively you may use XML file instead of Database. Check this
10 Steps to Building a Data Driven Navigation Menu with ASP.NET C# / VB.NET[^]
 
Share this answer
 
Hi,

Try this if could help...
C#
MenuItem homeSub = new MenuItem("Show Room", "", "", "Default.aspx");
MenuItem topMenu01 = new MenuItem("Show Room", "", "", "Default.aspx");
MenuItem subMenu01 = new MenuItem("Products", "", "", "Category.aspx");
topMenu01.ChildItems.Add(subMenu01);
NavigationMenu.Items.Add(topMenu01);
if (Page.User.Identity.IsAuthenticated)
{
   // For role setup user(s)
   if (Page.User.IsInRole("admin"))
   {
      MenuItem topMenu02 = new MenuItem("Admin. Opns.", "", "", "Default.aspx");
      MenuItem subMenu21 = new MenuItem("Categories", "", "", "Categories.aspx");
      topMenu02.ChildItems.Add(subMenu21);
      MenuItem subMenu22 = new MenuItem("Products", "", "", "Prod.aspx");
      topMenu02.ChildItems.Add(subMenu22);
      NavigationMenu.Items.Add(topMenu02);
    }
}


Please remember to vote if could help so that others may consider as an answer...

Regards,
 
Share this answer
 
v2
Comments
bhagyap 17-Jan-12 4:03am    
thanks for ur reply.. i also want to retain the state of menu control wen i debug it next time..
Al Moje 17-Jan-12 4:19am    
The above sample should be in code behind of
Site.Master.cs
in Page_Load...
bhagyap 17-Jan-12 5:24am    
i am talking abt the menu control,the next time wen i run my code i am unable to see the menu items which i previously added.. Please help wat am i missing?
Al Moje 17-Jan-12 21:07pm    
Hi,

See this link if could help...
http://www.dynamicdrive.com/dynamicindex1/
bhagyap 18-Jan-12 5:04am    
Thank u..

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