简体   繁体   English

ASP Control无序列表UL,带嵌套列表

[英]ASP Control unordered list UL with nested list

I tried Menu, TreeView, BulletedList, Repeater, HtmlGenericControl but without result. 我尝试了Menu,TreeView,BulletedList,Repeater,HtmlGenericControl,但没有结果。

What I would like to have is asp control which render something: 我想拥有的是呈现某些内容的asp控件:

<ul>
    <li><a href="#">Item 1</a></li>
    <li><a href="#">Item 2</a>
        <ul>
            <li><a href="#">Item 2.1</a></li>
        </ul>
    </li>
</ul>

For example menu it's render SPAN no UL. 例如,菜单使SPAN没有UL。

<asp:Menu runat="server">
     <Items>
          <asp:MenuItem Text="Item 1" />
          <asp:MenuItem Text="Item 2">
             <asp:MenuItem Text="Item 2.1" />
          </asp:MenuItem>
    </Items>
</asp:Menu>

I also tried 我也试过

<asp:Menu RenderingMode="MenuRenderingMode" />

But it's not working. 但这不起作用。 I'm using ASP.NET 3.5. 我正在使用ASP.NET 3.5。

I need create dynamic ul list and after click on item it will check in db if exist nested items and add them to clicked list as nested ul. 我需要创建动态ul列表,单击项目后,它将在db中检查是否存在嵌套项目,并将它们作为嵌套ul添加到单击的列表中。

I cant render whole menu at once because of performence. 由于性能原因,我无法一次渲染整个菜单。

Sorry if I'm not very clear. 对不起,如果我不太清楚。 Thanks for help. 感谢帮助。

You need to use nested repeaters: 您需要使用嵌套的中继器:

<asp:Repeater ID="rptFoo" runat="server" OnItemDataBound="rptFoo_ItemDataBound">
   <HeaderTemplate>
     <ul>
   </HeaderTemplate>
   <ItemTemplate>
       <li>
           <asp:HyperLink ID="lnkFoo" runat="server" />
           <asp:Repeater ID="rptBar" runat="server" OnItemDataBound="rptBar_ItemDataBound">
               <HeaderTemplate>
                  <ul>
               </HeaderTemplate>
               <ItemTemplate>
                    <li><asp:HyperLink ID="lnkBar" runat="server" /></li>
               </ItemTemplate> 
               <FooterTemplate>
                  </ul>
               </FooterTemplate>
           </asp:Repeater>
      </li>
   </ItemTemplate> 
   <FooterTemplate>
     </ul>
   </FooterTemplate>
</asp:Repeater>

And then in the ItemDataBound handler, you need to check whether each top level item has child elements, if it does, assign those elements to the sub repeater and databind, else hide it. 然后在ItemDataBound处理程序中,您需要检查每个顶级项目是否都有子元素(如果有),将这些元素分配给子转发器并进行数据绑定,否则将其隐藏。

protected void rptFoo_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
    {
        FooType obj = (FooType)e.Item.DataItem;
        Repeater rptBar = (Repeater)e.Item.FindControl("rptBar");
        if(obj.Children.Count() > 0)
        {
            rptBar.DataSource = obj.Children;
            rptBar.DataBind();
        }
        else
        {
            rptBar.Visible = false;
        }
    }
}

If you just need access to the ul and li elements from codebehind you can easily add a runat="server" attribute to them. 如果只需要从代码后面访问ulli元素,则可以轻松地向它们添加runat="server"属性。 The id attribute is necessary in order to be able to reference a single element: 为了能够引用单个元素, id属性是必需的:

<ul id="mainMenu" runat="server">
    <li id="mainMenuItem1" runat="server"><a href="#">Item 1</a></li>
    <li id="mainMenuItem2" runat="server"><a href="#">Item 2</a>
        <ul id="subMenu" runat="server">
            <li id="subMenuItem1" runat="server"><a href="#">Item 2.1</a></li>
        </ul>
    </li>
</ul>

This structure could also be build from codebehind using HtmlGenericControl s. 也可以使用HtmlGenericControl从代码隐藏中构建此结构。

Another option could be to use user controls: http://msdn.microsoft.com/en-us/library/y6wb1a0e%28v=vs.100%29.aspx 另一个选择是使用用户控件: http : //msdn.microsoft.com/zh-cn/library/y6wb1a0e%28v=vs.100%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM