How to Bind Menus in asp.menu control

Posted by Jayakumars under C# on 3/18/2017 | Points: 10 | Views : 301 | Status : [Member] [MVP] | Replies : 4
Hi

How to list collection binded asp.net menu control like DNF site
for ex: Home,articles etc

My Code this here then my list have more records then i bind my asp:menu control
i meet error which i have given below. any one guide how to
assign list menus assign asp:menu control

List<Menus> lstMenus;
<asp:Menu id=mnuid

mnuid.DataSource = lstMenus;
mnuid.DataBind();

This is error

An exception of type 'System.InvalidOperationException' occurred in System.Web.dll but was not handled in user code

Additional information: HierarchicalDataBoundControl only accepts data sources that implement IHierarchicalDataSource or IHierarchicalEnumerable.

Mark as Answer if its helpful to you


Responses

Posted by: A2H on: 3/19/2017 [Member] [MVP] Silver | Points: 25

Up
0
Down
You cannot assign items to menu control like we do in Gridview or repeater.You need to loop the list and add items like below
  foreach (Menus item in lstMenus)
{
mnuid.Items.Add(new MenuItem(item.MenuName.ToString()));
}


Sample Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Menu ID="mnuid" runat="server"></asp:Menu>

</div>
</form>
</body>
</html>

C#:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Sample Data to load the menu items
List<Menus> lstMenus = new List<Menus>();
Menus objMenu = new Menus();
objMenu.MenuName = "Menu1";
lstMenus.Add(objMenu);
Menus objMenu2 = new Menus();
objMenu2.MenuName = "Menu2";
lstMenus.Add(objMenu2);
Menus objMenu3 = new Menus();
objMenu3.MenuName = "Menu3";
lstMenus.Add(objMenu3);

//Menu1.DataSource = lstMenus;
//Menu1.DataBind();

//loop through the list and add items to menu control
foreach (Menus item in lstMenus)
{
mnuid.Items.Add(new MenuItem(item.MenuName.ToString()));
}
}
}

//Class created
public class Menus
{
public string MenuName { get; set; }
}


Thanks,
A2H
My Blog

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 3/19/2017 [Member] [MVP] Silver | Points: 25

Up
0
Down
If you want to load child menu then you can refer this link
https://www.codeproject.com/Tips/354696/Dynamically-populating-menu-items-from-the-databas

Thanks,
A2H
My Blog

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 3/20/2017 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi
A2H

thnaks for your reply

But my menus shows like this with Bootstrap then menuitems binded from Db in C#
can you guide me.

How to create menu like this
https://www.visualstudio.com/downloads/

Mark as Answer if its helpful to you

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 3/22/2017 [Member] [MVP] Silver | Points: 25

Up
0
Down
You can refer the below link to add menu from database
https://www.aspsnippets.com/Articles/Bootstrap-Responsive-ASPNet-Menu-control-for-Mobile-Phone-Tablet-and-Desktop-display.aspx

Thanks,
A2H
My Blog

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response