How to create a nested bulleted list

二次信任 提交于 2019-12-12 19:56:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!