asp.net 多级数据绑定菜单
我目前正在使用 asp.net 菜单控件从表父/子项加载。我遇到的问题是,如果这个孩子还有另一个孩子。从这个意义上说,我的代码是静态的,我似乎找不到更好的或“该”的方法来做到这一点。我已将站点地图视为数据源,但我不需要站点地图,并且觉得这对于我需要实现的目标来说太过分了。
foreach (ClassName option in list)
{
MenuItem module = new MenuItem(option.Description.ToLower(), "", "", option.Url + "?option=" + option.Optionid);
module.Selectable = true;
navigation.Items.Add(module);
//this is my second level
foreach (ClassName child in listfromparent(option.Optionid))
{
MenuItem childmenu = new MenuItem(child.Description.ToLower(), "", "", child.Url + "?option=" + child.Optionid);
module.ChildItems.Add(childmenu);
}
}
正如你所看到的,这有效,但只适用于 2 个级别:( 当然,我可以在 child 中放入另一个子级别来创建第三级别,但是如果有第四、第五级别怎么办?所以这就是为什么我需要它自己做。我注意到树视图有 onpopulate 但显然菜单没有。提前致谢。
I am currently using an asp.net menu control to load from a table parent/child items. The problem I am having is that if the child has another child. My code is kindof static in that sense and I can't seem to find a better or "the" way to do it. I have seen sitemap as datasources but i don't need a sitemap and feel that would just be overkill for what I need to achieve.
foreach (ClassName option in list)
{
MenuItem module = new MenuItem(option.Description.ToLower(), "", "", option.Url + "?option=" + option.Optionid);
module.Selectable = true;
navigation.Items.Add(module);
//this is my second level
foreach (ClassName child in listfromparent(option.Optionid))
{
MenuItem childmenu = new MenuItem(child.Description.ToLower(), "", "", child.Url + "?option=" + child.Optionid);
module.ChildItems.Add(childmenu);
}
}
as you can see this works but for 2 levels :(
and of course i could put another childlevel inside child to create the 3rd level but what if there is a 4th, 5th? So that's why I need it to do it itself. I noticed treeview has onpopulate but apparently Menu doesn't. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是您可以做到的一种方法。
也许您可以跳过中间步骤并将邻接列表直接映射到 MenuItems 树在 MenuItem 上有一些扩展方法。
但无论如何...
Default.aspx
Default.aspx.cs
MenuNode.cs
Here's one way you could do it.
Maybe you could skip that middle step and map the adjacency list straight to a tree of MenuItems, maybe with some extension methods on MenuItem.
But anyway...
Default.aspx
Default.aspx.cs
MenuNode.cs