问题
I am using ASP.net with C# and I would like to have a list that i can code dynamically to look like somewhat this:
- Menu Item A
- Sub Menu Link 1
- Sub Menu Link 2
- Sub Menu Link 3
so far i have managed to create the first past by doing this:
BulletedList MainUl = new BulletedList();
MainUl.DisplayMode = BulletedListDisplayMode.HyperLink;
ListItem item = new ListItem("Menu Item A", "http://www.google.com");
MainUl.Items.Add(item);
panel.Controls.Add(MainUl);
The problem is i do not know how to nest the other sub items under the main "Menu Item A"
Any help is greatly appreciated :)
回答1:
try with this code
<asp:Menu ID="Menu1" runat="server" StaticDisplayLevels="3">
<Items>
<asp:MenuItem Text="Menu Item A" Value="0">
<asp:MenuItem Text="Sub Menu Link 1" Value="1"></asp:MenuItem>
<asp:MenuItem Text="Sub Menu Link 2" Value="2"></asp:MenuItem>
<asp:MenuItem Text="Sub Menu Link 3" Value="3"></asp:MenuItem>
</asp:MenuItem>
</Items>
</asp:Menu>
Or this code
<asp:TreeView ID="MyTreeView" Runat="server">
<Nodes>
<asp:TreeNode Value="0" Expanded="True" Text="Menu Item A">
<asp:TreeNode Text="Sub Menu Link 1" Value="1" />
<asp:TreeNode Text="Sub Menu Link 2" Value="2" />
<asp:TreeNode Text="Sub Menu Link 3" Value="3" />
</asp:TreeNode>
</Nodes>
</asp:TreeView>
回答2:
Alternatively you can use DataControl
( DataList, Repeater, GridView) with BulletedList
. You should try Menu or TreeView control.
回答3:
Use Tree view
protected void Page_Load(object sender, EventArgs e)
{
TreeNode pn = new TreeNode("Menu Item A");
TreeNode cn1 = new TreeNode("<a href='http://www.google.com.pk/' >Sub Menu Link 1</a>");
TreeNode cn2 = new TreeNode("<a href='http://www.google.com.pk/' >Sub Menu Link 2</a>");
TreeNode cn3 = new TreeNode("<a href='http://www.google.com.pk/' >Sub Menu Link 3</a>");
TreeView tv = new TreeView();
tv.Nodes.Add(pn);
tv.Nodes[0].ChildNodes.Add(cn1);
tv.Nodes[0].ChildNodes.Add(cn2);
tv.Nodes[0].ChildNodes.Add(cn3);
panel.Controls.Add(tv);
}
Output:
Menu Item A
Sub Menu Link 1
Sub Menu Link 2
Sub Menu Link 3
来源:https://stackoverflow.com/questions/11700632/how-to-create-a-nested-bulleted-list