Click here to Skip to main content
15,890,350 members
Articles / Web Development / ASP.NET
Tip/Trick

Dynamically menu binding

Rate me:
Please Sign up or sign in to vote.
4.80/5 (2 votes)
29 Oct 2013CPOL 13.4K   3  
Hi friends..... Many of guys are trying or working on menus for binding it dynamically. i've the solution for ithere , i m putting sample code to

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Hi friends.

Many of guys are trying or working on menus for binding it dynamically. I've the solution for it.

Here, I'm putting sample code to create menu dynamically.

MenuTest.aspx

ASP.NET
<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal">
</asp:Menu>  

MenuTest.aspx.cs  

C#
static string constr = "SQLOLEDB;Data Source=ABC;Integrated Security=SSPI;Initial Catalog=XYZ";
OleDbConnection cn = new OleDbConnection(constr);  

protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
    PopulateRootLevel();
}  

private void PopulateRootLevel()
{
  OleDbCommand objCommand = new OleDbCommand(@"select id,title,(select count(*) FROM SampleCategories WHERE parentid=sc.id) childnodecount FROM SampleCategories sc where parentID IS NULL", cn);
  OleDbDataAdapter da = new OleDbDataAdapter(objCommand);
  DataTable dt = new DataTable();
  da.Fill(dt);
  PopulateNodes(dt, Menu1.Items);
}

private void PopulateSubLevel(int parentid, MenuItem parentMenu)
{
  OleDbCommand objCommand = new OleDbCommand(@"select id,title,(select count(*) FROM SampleCategories WHERE parentid=sc.id) childnodecount FROM SampleCategories sc where parentID="+parentid+"", cn);
  OleDbDataAdapter da = new OleDbDataAdapter(objCommand);
  DataTable dt = new DataTable();
  da.Fill(dt);
  PopulateNodes(dt, parentMenu.ChildItems);
}

private void PopulateNodes(DataTable dt, MenuItemCollection items)
{
  foreach (DataRow dr in dt.Rows)
  {
    MenuItem mi = new MenuItem 
    {
      Text = dr["title"].ToString();  
      Value = dr["id"].ToString();
    }
    items.Add(mi);
    //If node has child nodes, then enable on-demand populating
    bool flag = ((int)(dr["childnodecount"]) > 0);
    if (flag)
    {
      menuCreate(mi);
    }
  }
}
private void menuCreate(MenuItem m)
{
  MenuEventArgs e = new MenuEventArgs(m);
  PopulateSubLevel(Int32.Parse(e.Item.Value), e.Item);
}

Here is the database script:

For schema generating:

SQL
 CREATE TABLE [dbo].[SAMPLECATEGORIES](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [parentid] [int] NULL,
    [title] [nvarchar](255) COLLATE SQL_SwedishStd_Pref_CP1_CI_AS NOT NULL
)  

For testing, data samples:

SQL
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (1, NULL, N'Category 1')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (2, NULL, N'Category 2')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (3, NULL, N'Category 3')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (4, 1, N'Category 1.1')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (5, 1, N'Category 1.2')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (6, 2, N'Category 2.1')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (7, 2, N'Category 2.2')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (8, 2, N'Category 2.3')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (9, 7, N'Category 2.2.1')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (10, 7, N'Category 2.2.2')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (11, 10, N'Category 2.2.2.1')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (12, 6, N'Category 2.1.1')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (13, 3, N'Category 3.1')
INSERT INTO [dbo].[SAMPLECATEGORIES] ([id], [parentid], [title]) VALUES (14, 7, N'Category 2.2.3'

I hope this article will help you.

After the day of the research I've made this code and it is very helpful to others.

Edit History: 

29-Oct-2013: Changed type to Tip/Trick and minor formatting corrections 

This article was originally posted at http://wiki.asp.net/page.aspx/309/menu

License

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


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --