【问题标题】:Making an href tag dynamc in VB在VB中制作一个href标签动态
【发布时间】:2020-03-24 01:40:09
【问题描述】:

我是 VB 的新手,所以如果我的术语在 VB 方面不是最好的,我提前道歉。

我有一个 href 链接,我目前正在尝试使其动态化。以前我尝试使用asp: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】:

    不清楚你试图做什么,但我希望这是你想要的:

    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
    

    【讨论】:

    • 字符串生成器是否是动态创建 href 的正确方法?
    • 如果你需要给定的字符串作为这个函数的结果作为另一个控件的 InnerHtml 并且生成的“动态”控件由客户端管理(通过 Javascript)是的。否则,如果您想通过 .NET 访问,则需要通过代码创建 Asp 控件
    猜你喜欢
    • 2016-04-19
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 2012-03-11
    • 1970-01-01
    相关资源
    最近更新 更多