Создание динамического тега href c в VB - PullRequest
1 голос
/ 23 марта 2020

Я новичок в VB, поэтому заранее прошу прощения, если моя терминология не самая лучшая, когда дело доходит до VB.

У меня есть ссылка href, которую я сейчас пытаюсь сделать динамически c. Ранее я пытался использовать asp:HyperLink, но у меня были проблемы с HyperLink, поскольку я не мог связать заголовок и продолжал видеть example-link.com вместо VIEW ARTICLE.

Я провел некоторое исследование и, похоже, что подход с использованием строителя строк будет способом получить ожидаемый результат. В данный момент у меня возникают проблемы со строителем строк.

Вот краткое изложение того, что я пытаюсь выполнить sh


Пример ссылки на статус c что я пытаюсь воссоздать динамически. <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> Кодовое обозначение для 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

1 Ответ

0 голосов
/ 23 марта 2020

Не ясно, что вы пытаетесь сделать, но я надеюсь, что это то, что вы хотите:

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
...