在VB中制作href标签动态

问题描述 投票:1回答:1

我是VB的新手,所以如果我的术语不是VB最好的话,我先向您道歉。

我有一个href链接,目前正在尝试使之动态。以前,我尝试使用asp:HyperLink,但是由于无法链接标题,我一直在使用HyperLink遇到问题,并且一直看到example-link.com而不是VIEW ARTICLE

我已经进行了一些研究,并且看来,使用字符串构建器方法将是获得期望结果的方法。我目前在字符串生成器方面遇到问题。

这里是我要完成的工作的分解


我尝试动态重新创建的静态链接示例。 <i class="fa fa-chevron-right"></i><a href="/how-to-develop/" target="_self" title="How to Develop">VIEW ARTICLE</a>


[我尝试使用ASP:HyperLink: <asp:HyperLink ID="MyListUrl" runat="server">View Article</asp:HyperLink> 超链接的代码隐藏]

Public Class Links
    Public Property MyListId As Integer
    Public Property MyListRecommendation As String 
    Public Property MyListSummary As String 
    Public Property MyListUrl As String 
End Class

 Protected Sub InitializeTrendsListsObject() 

 If MyLists.MyListId = 1 Then ' How to Develop
      MyLists.MyListUrl = "/how-to-develop/"
 ElseIf MyLists.MyListId = 2 Then ' How to Code
      MyLists.MyListUrl = "/how-to-code/"      
End Sub

我尝试使用字符串生成器: <i class="fa fa-chevron-right"></i><asp:Literal ID="ltMyListURL" runat="server"></asp:Literal>

后面的代码

Protected Overloads Function BuildRecommendation (ByVal reports As DataTable, ByVal counter As Integer, ByVal isExec As Boolean) As String

        Dim htmlBuilder As New StringBuilder
        Dim endLoop As Integer = 1

        If isExec Then

            For Each report As DataRow In reports.Rows

                If Not endLoop.Equals(counter) Then

    htmlBuilder.AppendLine("<p class='trends-list-item-summary'>" + report(2).ToString() + "... <a 
    class='trends-list-item-more' href='" + MyList.MyListUrl+ report(3).ToString() + "/' 
   target='_blank' title='" + report(0).ToString() + "'>View Article</a></p>")

                    endLoop += 1 ' increment controller by 1
                Else
                    Exit For
                End If

            Next

            ' Close content container
            htmlBuilder.AppendLine("</div>")

        End If

        Return htmlBuilder.ToString()
    End Function

vb.net dynamic href stringbuilder
1个回答
0
投票

尚不清楚您尝试做什么,但我希望这是您想要的:

Protected Overloads Function BuildRecommendation(ByVal reports As DataTable, ByVal counter As Integer, ByVal isExec As Boolean) As String

    Dim htmlBuilder As New StringBuilder
    Dim endLoop As Integer = 1

    Dim sConst As String = "<i class='fa fa-chevron-right'>{0}</i> <a href='{1}' target='_self' title='{2}'>VIEW ARTICLE</a>"

    If isExec Then

        'Open the content container
        htmlBuilder.AppendLine("<div>")

        For Each report As DataRow In reports.Rows

            If Not endLoop.Equals(counter) Then

                htmlBuilder.AppendLine(String.Format(sConst,
                                                     report(2).ToString(),
                                                     MyList.MyListUrl & report(3).ToString(),
                                                     report(0).ToString()))

                endLoop += 1 ' increment controller by 1
            Else
                Exit For
            End If

        Next

        ' Close content container
        htmlBuilder.AppendLine("</div>")

    End If

    Return htmlBuilder.ToString()
End Function
© www.soinside.com 2019 - 2024. All rights reserved.