【问题标题】:How to create a bulletlist with hyperlinks on code behind?如何在后面的代码上创建带有超链接的项目符号列表?
【发布时间】:2017-07-11 09:03:52
【问题描述】:

我正在使用 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

这段代码输出如下:

试试两个

我也试过这个代码:

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>

【问题讨论】:

  • 您不能在 ListItem 中创建超链接。最简单的方法是创建一个包含所有 li 元素及其链接的字符串,并将其添加到文字控件。

标签: asp.net vb.net webforms


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 2020-01-18
    • 1970-01-01
    • 2017-05-15
    • 2023-03-14
    相关资源
    最近更新 更多