hie buddy , i have made dynamic menu for 3 level but if you want you can make recursive. for that you can make table in database which will store recurssive data in same table.
You can create table in database as per below example
Please Dont forget to Vote.
<table>
<tr>
<th>
ID
</th>
<th>
MenuName
</th>
<th>
ParentID
</th>
</tr>
<tr>
<td>
1
</td>
<td>
MainMenu1
</td>
<td>
</td>
</tr>
<tr>
<td>
2
</td>
<td>
MainMenu1
</td>
<td>
</td>
</tr>
<tr>
<td>
3
</td>
<td>
SubMenu1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
4
</td>
<td>
InnerMenu
</td>
<td>
3
</td>
</tr>
<tr>
<td>
5
</td>
<td>
SubMenu2
</td>
<td>
2
</td>
</tr>
</table>
<![CDATA[<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DynamicMenu.aspx.cs" Inherits="testApps.DynamicMenu" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div id="divMenu" runat="server" >
</div>
<asp:Menu ID="Menu1" runat="server">
</asp:Menu>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Linq;
using testApps;
namespace testApps
{
public partial class DynamicMenu : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CreateMenu();
}
private void CreateMenu()
{
TypedDataTable dt = DatabaseMenuTable();
var MenuRows = from TypedDataRow p in dt.Rows
where p.ParentMenuID == ""
select new
{
ID = p.ID,
MenuName = p.MenuName,
ParentID = p.ParentMenuID,
child = from TypedDataRow c in dt.Rows
where c.ParentMenuID == p.ID
select
new
{
ID = c.ID,
MenuName = c.MenuName,
ParentID = c.ParentMenuID,
child = from TypedDataRow d in dt.Rows
where d.ParentMenuID == c.ID
select
new
{
ID = d.ID,
MenuName = d.MenuName,
ParentID = d.ParentMenuID,
}
}
};
Menu DynamicMenu = new Menu();
for (int i = 0; i < MenuRows.Count(); i++)
{
MenuItem MainMenu = new MenuItem();
MainMenu.Value = MenuRows.ElementAt(i).ID;
MainMenu.Text = MenuRows.ElementAt(i).MenuName;
DynamicMenu.Items.Add(MainMenu);
if (MenuRows.ElementAt(i).child.Count() != 0)
{
for (int j = 0; j < MenuRows.ElementAt(i).child.Count(); j++)
{
MenuItem SubMenu = new MenuItem();
SubMenu.Value = MenuRows.ElementAt(i).child.ElementAt(j).ID;
SubMenu.Text = MenuRows.ElementAt(i).child.ElementAt(j).MenuName;
DynamicMenu.Items[i].ChildItems.Add(SubMenu);
for (int k = 0; k < MenuRows.ElementAt(i).child.ElementAt(j).child.Count(); k++)
{
MenuItem InnerMenu = new MenuItem();
InnerMenu.Value = MenuRows.ElementAt(i).child.ElementAt(j).ID;
InnerMenu.Text = MenuRows.ElementAt(i).child.ElementAt(j).MenuName;
DynamicMenu.Items[i].ChildItems[j].ChildItems.Add(InnerMenu);
}
}
}
}
divMenu.Controls.Add(DynamicMenu);
}
protected TypedDataTable DatabaseMenuTable()
{
TypedDataTable menuTable = new TypedDataTable();
TypedDataRow row;
row = menuTable.GetNewRow();
row.ID = "1";
row.MenuName = "MainMenu1";
row.ParentMenuID = "";
menuTable.Add(row);
row = menuTable.GetNewRow();
row.ID = "2";
row.MenuName = "MainMenu2";
row.ParentMenuID = "";
menuTable.Add(row);
row = menuTable.GetNewRow();
row.ID = "3";
row.MenuName = "SubMenu";
row.ParentMenuID = "1";
menuTable.Add(row);
row = menuTable.GetNewRow();
row.ID = "4";
row.MenuName = "innerMenu";
row.ParentMenuID = "3";
menuTable.Add(row);
row = menuTable.GetNewRow();
row.ID = "5";
row.MenuName = "SubMenu2";
row.ParentMenuID = "2";
menuTable.Add(row);
return menuTable;
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace testApps
{
public class TypedDataTable:DataTable
{
public event TypedDataRowChangedDlgt TypedDataRowChanged;
public TypedDataRow this[int idx]
{
get { return (TypedDataRow)Rows[idx]; }
}
public TypedDataTable()
{
Columns.Add(new DataColumn("ID", typeof(string)));
Columns.Add(new DataColumn("MenuName", typeof(string)));
Columns.Add(new DataColumn("ParentMenuID", typeof(string)));
}
public void Add(TypedDataRow row)
{
Rows.Add(row);
}
public void Remove(TypedDataRow row)
{
Rows.Remove(row);
}
public TypedDataRow GetNewRow()
{
TypedDataRow row = (TypedDataRow)NewRow();
return row;
}
protected override Type GetRowType()
{
return typeof(TypedDataRow);
}
protected override DataRow NewRowFromBuilder(DataRowBuilder builder)
{
return new TypedDataRow(builder);
}
protected override void OnRowChanged(DataRowChangeEventArgs e)
{
base.OnRowChanged(e);
TypedDataRowChangedEventArgs args = new TypedDataRowChangedEventArgs(e.Action, (TypedDataRow)e.Row);
OnTypedDataRowChanged(args);
}
protected virtual void OnTypedDataRowChanged(TypedDataRowChangedEventArgs args)
{
if (TypedDataRowChanged != null)
{
TypedDataRowChanged(this, args);
}
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace testApps
{
public class TypedDataRow : DataRow
{
public string ID
{
get { return (string)base["ID"]; }
set { base["ID"] = value; }
}
public string MenuName
{
get { return (string)base["MenuName"]; }
set { base["MenuName"] = value; }
}
public string ParentMenuID
{
get { return (string)base["ParentMenuID"]; }
set { base["ParentMenuID"] = value; }
}
internal TypedDataRow(DataRowBuilder builder)
: base(builder)
{
ID = String.Empty;
MenuName = String.Empty;
ParentMenuID = String.Empty;
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace testApps
{
public delegate void TypedDataRowChangedDlgt(TypedDataTable sender, TypedDataRowChangedEventArgs args);
public class TypedDataRowChangedEventArgs
{
protected DataRowAction action;
protected TypedDataRow row;
public DataRowAction Action
{
get { return action; }
}
public TypedDataRow Row
{
get { return row; }
}
public TypedDataRowChangedEventArgs(DataRowAction action, TypedDataRow row)
{
this.action = action;
this.row = row;
}
}
}
%>