【问题标题】:Add img to ahref tag将图像添加到 href 标签
【发布时间】:2012-03-04 18:32:36
【问题描述】:

我希望从我的代码隐藏中完成以下格式:

<a href="">
<img src=""/>
</a>

我已经这样做了,但我无法将图像添加到&lt;a href&gt; 谁能告诉我该怎么做?

这是我的代码:

Dim urls As New List(Of String)
                urls.Add("Downloads/" & Session("tempDir").ToString & "/" & filename)
                For Each imageURL As String In urls
                    Dim img = New System.Web.UI.WebControls.Image()
                    img.ImageUrl = imageURL
                    img.Attributes.Add("src", "Downloads/" & Session("tempDir").ToString & "/" & filename)
                    img.Attributes.Add("rel", "group2")
                    img.Width = 75
                    img.Height = 50
                    img.CssClass = "clickImage"
                    img.ImageAlign = HorizontalAlign.Left
                    Me.FinalPreview.Controls.Add(img)
                Next

我发现了这个:

Dim anchor As New HtmlGenericControl("a")
anchor.Attributes.Add("href", "#")

但我无法理解如何向其中添加 img 如果我添加控件,则总 img 不会显示。

【问题讨论】:

    标签: asp.net html vb.net image href


    【解决方案1】:

    您可以使用Literal 控制来完成此操作

    在页面上放置一个文字控件,然后像这样添加它的文本...

    Dim strb as New StringBuilder()    
    For Each imageURL As String In urls
       strb.Append("<a href='#'><img src='").Append(imageURL).Append("' /></a>");
    Next
    litImage.Text = strb.ToString()
    

    【讨论】:

    • @Naveed-抱歉,回复晚了。感谢您的回答。我对此有一个小问题,实际上文字控制是一个好主意!但是我有一个 div 控件,我需要在其中添加图像。如果我将此控件添加到该 div 中,那么我认为该字符串无法转换为 web.ui.controls。那么如何将它添加到我的 div 中。是一个文本,我可以添加一个图像而不是多个图像。
    • 我认为您正在尝试将字符串分配给 Literal 本身。尝试将字符串分配给 Literal.Text
    • 我明白了! :)谢谢再次。
    【解决方案2】:

    Image 有一个名为 ImageUrl 的特殊属性,用于将 URL 设置为。

    【讨论】:

    • @Vladimir-这就是我在上面所做的,但我需要在锚标记中添加 img。
    • ImageUrl 应该设置图像标签的src。它不会在图像标签周围添加锚控制...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-02
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多