Click here to Skip to main content
15,917,538 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hello friends,

I have an application which requires Dynamic menu generation. Where i can add menu items from database at runtime.


Thanks in advance.
Posted

You can bind the datasource to Menu control, That's all.

Populating Menu Control in ASP.NET 2.0 - using different data sources[^]
 
Share this answer
 
If you want to add menu items by your self, you can make a menu table, that submenus has parent menuid field and parents be null in this field. and use something like this code in pageload :
C#
void BindParentMenu()
   {
       try
       {

           Menu1.Items.Clear();

           //Add all items with null parentID
           SqlDataReader sdr = df.Get_All_ParentMainMenu();
           while (sdr.Read())
           {
               MenuItem mBase = new MenuItem();
               mBase.Value = sdr["MenuID"].ToString();
               mBase.Text = sdr["Title"].ToString();
               mBase.NavigateUrl = sdr["Url"].ToString();
               if (sdr["Url"].ToString() == "#")
               {
                   mBase.NavigateUrl = Request.Url.AbsoluteUri + "#";
               }
               SubMenu(mBase);
               Menu1.Items.Add(mBase);
                           }
           sdr.Close();
       }
       catch (Exception)
       {
       }
   }

  void SubMenu(MenuItem mBase)
   {
       try
       {
           //All items that their parent is mBase
           SqlDataReader sdr = df.Get_All_Menu_Child(Convert.ToInt32(mBase.Value));
           while (sdr.Read())
           {
               MenuItem mChild = new MenuItem();
               mChild.Value = sdr["MenuID"].ToString();
               mChild.Text = sdr["Title"].ToString();
               mChild.NavigateUrl = sdr["Url"].ToString();
               if (sdr["Url"].ToString() == "#")
               {
                   mChild.NavigateUrl = Request.Url.AbsoluteUri + "#";
               }
               mBase.ChildItems.Add(mChild);
               SubMenu(mChild);
           }
           sdr.Close();
       }
       catch (Exception)
       {
       }
   }
 
Share this answer
 
v2
You have to make sure your database table is capable of providing the info necessary to fully configure the menu, which means you need more than just a cursory knowledge of the and MenuItem class. I have my own ideas about how to do it, but it would be worthy of an article, and much too extensive to fit into a quick answer.

Beyond that Google is where you want to look. Here's a Codeproject article I found using the search term "C# create menu dynamically":

C# Creating Dynamic Menus[^]
 
Share this answer
 
A similar question was asked earlier. The answers here may help you -
Dynamic Menu Creation.........asp.net[^]
Dynamic Cascading Popup Menus -- impossible[^]
 
Share this answer
 
v2

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