Friday, July 23, 2010

Dynamic menu control....

In aspx page:

Take asp:menu control....And add bellow code in code behind...

Code behind:

private void loadCategory()
{
DataTable category = new DataTable();

category = CategoryService.Gettopcategory();

DataTable subcat = new DataTable();
subcat = CategoryService.Getsubcategory();

DataView categoryView = new DataView(category);
DataView childCategoryView = new DataView(subcat);



if (categoryView.Count > 0)
{
foreach (DataRowView categoryRowView in categoryView)
{
MenuItem parentMenu = new MenuItem();
parentMenu.Text = categoryRowView["maincategory"].ToString();
parentMenu.Value = categoryRowView["categoryid"].ToString();

childCategoryView.RowFilter = "categoryid=" + (int)categoryRowView["categoryid"];


foreach (DataRowView childcategoryRow in childCategoryView)
{

MenuItem childMenu = new MenuItem();
childMenu.Text = childcategoryRow["subcategory"].ToString();
childMenu.Value = childcategoryRow["subid"].ToString();
//childMenu.NavigateUrl = "~/Default.aspx?CategoryID=" + childMenu.Value;
parentMenu.ChildItems.Add(childMenu);
//}
}
Menu2.Items.Add(parentMenu);
}

}


}

No comments:

Post a Comment