Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Dynamically menu binding

0.00/5 (No votes)
29 Oct 2013 1  
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:Menu ID="Menu1" runat="server" Orientation="Horizontal">
</asp:Menu>  

MenuTest.aspx.cs  

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:

 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:

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 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here