Generate Menus in Asp.net Dynamically

Self-Innovator
Posted by Self-Innovator under ASP.NET category on | Points: 40 | Views : 1719
public DataSet GetMenuList()
{
SqlConnection cnn = new SqlConnection(Conn);
SqlDataAdapter adaParent = new SqlDataAdapter("Select * from tbParentMenu", cnn);--It binds the parent Menu
SqlDataAdapter adaChild = new SqlDataAdapter("Select * from tbChildMenu", cnn);--It Binds the ChildMenu
DataSet ds = new DataSet();
adaParent.Fill(ds,"tbParentMenu");
adaChild.Fill(ds, "tbChildMenu");
ds.Relations.Add("SubMenus",ds.Tables["tbParentMenu"].Columns["ParentID"],ds.Tables["tbChildMenu"].Columns["ParentID"]);--Clubing Both tables as a Dataset
return ds;
}

public void PopulateMenu()
{
DataSet dst = GetMenuList();
foreach(DataRow mstdr in dst.Tables["tbParentMenu"].Rows)--Binding Parent Menus
{
MenuItem mstItem = new MenuItem((string)mstdr["Text"]);
menuBar.Items.Add(mstItem);//menuBar ID of the Cntrl
foreach (DataRow childRow in mstdr.GetChildRows("SubMenus"))
{
MenuItem childItem = new MenuItem((string)childRow["Description"]);--Binding Child menus of Specific parent
mstItem.ChildItems.Add(childItem);
}
}
}
In Page Load Call
PopulateMenu()

Make Sure ur table Design with ForeignKey as ParentID in tbChildmenu

Comments or Responses

Login to post response