如何创建一个包含代码背后超链接的bulletlist?

时间:2017-07-11 09:03:52

标签: asp.net vb.net webforms

我正在使用VB.NET处理旧的webform应用程序,并且我将创建一个项目符号列表,在每个项目中都有一个超链接。我试过用三件事。

尝试一个

首先,我试图制作一串我的超链接。

Private Sub MakeSubNavigation(lst As List(Of clsProductCategory), placeholder As PlaceHolder)

    Dim list As BulletedList = New BulletedList()

    For Each category As clsProductCategory In lst

        Dim coll As ListItem = New ListItem()
        coll.Text = "<a href=""/" & category.CategoryId & """>" & category.Name(langId) & "</a>"
        list.Items.Add(coll)
    Next

    placeholder.Controls.Add(list)
End Sub

此代码输出:

enter image description here

尝试两个

我也试过这段代码:

Private Sub MakeSubNavigation(lst As List(Of clsProductCategory), placeholder As PlaceHolder)

    Dim list As BulletedList = New BulletedList()

    For Each category As clsProductCategory In lst

        Dim coll As ListItem = New ListItem()

        coll.Text = (New HyperLink() With {
            .NavigateUrl = "/" & category.CategoryId,
            .Text = category.Name(langId)
        }).ToString()

        list.Items.Add(coll)
    Next

    placeholder.Controls.Add(list)
End Sub

由于ToString()方法返回System.Web.UI.WebControls.HyperLink,因此无法正常工作。没有属性或方法可以将控件添加到我可以使用的ListItem

尝试三个

这会使列表正确但没有超链接(HTML a - tag)

Private Sub MakeSubNavigation(lst As List(Of clsProductCategory), placeholder As PlaceHolder)

    Dim list As BulletedList = New BulletedList()

    For Each category As clsProductCategory In lst

        Dim coll As ListItem = New ListItem(category.Name(langId), String.Format("../{0}", category.CategoryId))

        list.Items.Add(coll)
    Next

    placeholder.Controls.Add(list)
End Sub

ascx代码

这是ascx文件中的代码:

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="navigation.ascx.vb" Inherits="components_navigation" %>
<asp:Repeater ID="NavRepeater" runat="server">
  <HeaderTemplate>
    <ul class="list-inline">
  </HeaderTemplate>
  <ItemTemplate>
    <li id="Item" runat="server">
      <a id="Link" runat="server">
          <asp:Literal ID="Text" runat="server" />
       </a>
      <asp:PlaceHolder ID="SubNav" runat="server" /> <!-- variable placeholder in code -->
    </li>
  </ItemTemplate>
  <FooterTemplate>
    </ul>
  </FooterTemplate>
</asp:Repeater>

问题

我现在的问题是:如何添加到ListItem的超链接,以便我在浏览器中有下一个输出:

<ul>
    <li><a href="#">Bladgroenten</a></li>
    <li><a href="#">Koolsoorten</a></li>
    <li><a href="#">Paddenstoelen</a></li>
    <li><a href="#">Peulvruchten</a></li>
    <li><a href="#">Stengelgroenten</a></li>
    <li><a href="#">Vruchtgroenten</a></li>
    <li><a href="#">Wortel- en knolgewassen</a></li>
    <li><a href="#">Fruit</a></li>
</ul>

1 个答案:

答案 0 :(得分:1)

通过@VDWWD的评论,我试过这段代码,这很有效。

Private Sub MakeSubNavigation(categories As List(Of clsProductCategory), placeholder As PlaceHolder)

    Dim literal As Literal = New Literal()
    Dim builder As StringBuilder = New StringBuilder()

    builder.Append("<ul>")

    For Each category As clsProductCategory In categories

        builder.Append("<li><a href=""/")
        builder.Append(category.CategoryId)
        builder.Append(""">")
        builder.Append(category.Name(langId))
        builder.Append("</a></li>")
    Next

    builder.Append("</ul>")
    literal.Text = builder.ToString()

    placeholder.Controls.Add(literal)
End Sub