关于转发器中的itemdatabound

时间:2009-07-16 06:57:13

标签: asp.net vb.net

我在页面加载中有以下代码

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            GetDetails()
            PopulateRepeater()
        End If
    End Sub

Sub PopulateRepeater()
        Dim dt As DataTable = GetDetails()
        Dim dtDoc As DataTable = objdoc.GetDocDetails(Session("RegID"))
        If dtDoc.Rows.Count > 0 Then
            Dim strUserName As String = dt.Rows(0)("Name")
            Dim files As IList(Of FileInfo) = New List(Of FileInfo)()
            Dim filters As String = "*.jpg;*.png;*.gif"
            For Each filter As String In filters.Split(";"c)
                Dim fit As FileInfo() = New DirectoryInfo(Me.Server.MapPath("../SiteImages/" & strUserName & "/" & Session("RegID") & "/")).GetFiles(filter)
                For Each fi As FileInfo In fit
                    files.Add(fi)
                Next
            Next
            strPath = Server.MapPath("../SiteImages/" & strUserName & "/" & Session("RegID") & "/")
            Me.Repeater1.DataSource = files
            Me.Repeater1.DataBind()
        End If

        End Sub

我在itemdatabound

中有以下代码
Dim ThViewr As Bright.WebControls.ThumbViewer = DirectCast(e.Item.FindControl("Th1"), Bright.WebControls.ThumbViewer)
        Dim dtUser As DataTable = GetDetails()
        Dim dtDoc As DataTable = objdoc.GetDocDetails(Session("RegID"))
        Dim strUserName As String = dtUser.Rows(0)("Name")
        If dtDoc.Rows.Count > 0 Then
            For i As Integer = 0 To dtDoc.Rows.Count - 1
                Dim ImagePath As String = "../SiteImages/" & strUserName & "/" & Session("RegID") & "/" + dtDoc.Rows(i)("ImageName")
                ThViewr.ImageUrl = ImagePath
            Next

        End If

我的aspx包含

<div style="clear:both;">
                <asp:Repeater ID="Repeater1" runat="server" >
                    <ItemTemplate>
                        <span style="padding:2px 10px 2px 10px">

                            <bri:ThumbViewer Id="Th1"  runat="server" Height="100px" Width="100px"/>
                        </span>
                    </ItemTemplate>
                </asp:Repeater>
            </div> 

如果imagePath =“../ SiteImages / Ram / PR / First.jpg”意味着文件夹PR只包含3张图像,即First.jpg,Second.jpg和Third.jpg。

现在有了上面的代码,三张图片即将来临,但是Third.jpg正在重复3次.First.jpg和Second.jpg没有来。任何人都可以帮忙解决这个问题。

1 个答案:

答案 0 :(得分:1)

对于绑定列表中的每个对象,都会引发一次ItemDataBound事件,因此在您的情况下将触发三次;每个文件一次。您不应该遍历数据表,而是从事件参数中获取当前项目。

更新:仔细查看代码,我发现它有些令人困惑。将FileInfo对象列表绑定到转发器,但在绑定项目时从DataTable获取数据。我猜你想要显示找到的文件,我认为ItemDataBound中的以下代码会为你做这些:

Dim ThViewr As Bright.WebControls.ThumbViewer = DirectCast(e.Item.FindControl("Th1"), Bright.WebControls.ThumbViewer)
Dim dtUser As DataTable = GetDetails()
Dim strUserName As String = dtUser.Rows(0)("Name")
Dim ImagePath As String = "../SiteImages/" & strUserName & "/" & Session("RegID") & "/" + DirectCast(e.Item.DataItem, FileInfo).Name
ThViewr.ImageUrl = ImagePath