0

我有一个列表视图:

 <asp:ListView ID="ListView1" runat="server">
           <ItemTemplate>
                 <asp:ImageButton ID="ImageButton1" runat="server" />
           </ItemTemplate>
 </asp:ListView> 

我有VB中图像的链接:

  Dim dirInfo As string="D:\rbi\images\emoticons\"
  Dim filenames As List(Of String) = dirInfo.GetFiles().[Select](Function(j) j.Name).ToList()

我从目录而不是从数据库中获取链接和文件名...那么如何在列表视图中绑定这些链接以显示所有图像?

4

2 回答 2

0

将图像路径绑定到 的一种方法是拥有一个包含要在绑定中使用的属性ListView的对象,如下所示:PublicEval()

代码隐藏:

Public Class ImageBinder
    Public Property path() As String
        Get
            Return m_path
        End Get
        Set
            m_path = Value
        End Set
End Property
Private m_path As String
End Class

您现在需要有代码来创建ImageBinder对象列表,而不仅仅是字符串列表。

现在在您的标记中,您可以在标记中引用该path属性ListView,如下所示:

<asp:ListView ID="ListView1" runat="server">
    <ItemTemplate>
        <asp:Image id="Image1" runat="server" ImageUrl='<%# Eval("path") %>' />
    </ItemTemplate>
</asp:ListView> 

注意:这将负责绑定,但您将不得不处理的一个问题是,如果D:\rbi\images\emoticons\您在问题中提供的示例路径 ( ) 是正确的,那么它将无法在 ASP.NET 的范围内工作,因为安全限制不是要使用的应用程序虚拟目录之外的路径。您将需要创建一个虚拟目录并将其用作您希望在ListView.

于 2013-11-08T16:05:29.753 回答
0

请尝试以下完整解决方案:

在 aspx 页面中:

   <asp:ListView ID="ListView1" runat="server">
       <ItemTemplate>
             <asp:ImageButton ID="ImageButton1" runat="server" ToolTip='<%#Eval("Name")%>'/>
       </ItemTemplate>
   </asp:ListView>

在 aspx.vb 页面中:

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim di As New IO.DirectoryInfo(Server.MapPath("Images/emoticons/"))
        Dim diar1 As IO.FileInfo() = di.GetFiles()
        Dim dra As IO.FileInfo

        Dim FileList As New List(Of IO.FileInfo)
        'list the names of all files in the specified directory
        For Each dra In diar1
            FileList.Add(dra)
        Next

        ListView1.DataSource = FileList
        ListView1.DataBind()
    End Sub

    Protected Sub ListView1_ItemDataBound(sender As Object, e As  System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView1.ItemDataBound
        Dim ImageButton1 As ImageButton
        If e.Item.ItemType = ListViewItemType.DataItem Then
            Dim rowView As IO.FileInfo = e.Item.DataItem()
            Dim FileName As String = rowView.Name.ToString()
            ImageButton1 = e.Item.FindControl("ImageButton1")
            ImageButton1.ImageUrl = "~/Images/emoticons/" & FileName
        End If
    End Sub
于 2013-11-08T16:16:20.217 回答