1

我有一个Page_Load绑定到repeater.

我正在将结果写入 ItemDataBound 中的页面,但是当它是最后一行数据时,我需要它做一些稍微不同的事情。

如何从转发器的 ItemDataBound 中访问 Page_Load 中数据源的行数?

我试过了:

Dim iCount As Integer
iCount = (reWorkTags.Items.Count - 1)
If e.Item.ItemIndex = iCount Then
    'do for the last row
Else
    'do for all other rows
End If

但对于每一行,e.Item.ItemIndex 和 iCount 都相等。

谢谢你的帮助。J。

4

4 回答 4

4

但对于每一行,e.Item.ItemIndex 和 iCount 都相等。

这是因为这些项目仍然具有约束力。绑定时,Count 将是当前项目索引的 +1。

我认为最好在repeater完全绑定后执行此操作。

因此,您可以将以下内容添加到您的Page_Load

rep.DataBind()

For each item as repeateritem in rep.items
   if item.ItemIndex = (rep.Items.Count-1)
       'do for the last row
   else
       'do for all other rows
   end if
Next

注意:我刚刚添加rep.DataBind()显示这应该在转发器绑定后运行。

于 2011-12-09T12:45:36.687 回答
1

试图避免使用 Sessions,但最终让它与一个一起工作。

我刚刚创建了一个行数会话,并且可以从 ItemDataBound 访问它。

Protected Sub reWorkTags_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles reWorkTags.ItemDataBound


    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then

        Dim rowView As System.Data.DataRowView
        rowView = CType(e.Item.DataItem, System.Data.DataRowView)

        Dim link As New HyperLink
        link.Text = rowView("tag")
        link.NavigateUrl = rowView("tagLink")
        link.ToolTip = "View more " & rowView("tag") & " work samples"

        Dim comma As New LiteralControl
        comma.Text = ", "

        Dim workTags1 As PlaceHolder = CType(e.Item.FindControl("Linkholder"), PlaceHolder)

        If e.Item.ItemIndex = Session("iCount") Then
            workTags1.Controls.Add(link)
        Else
            workTags1.Controls.Add(link)
            workTags1.Controls.Add(comma)
        End If

    End If

End Sub
于 2011-12-09T13:11:05.930 回答
1

这是一个老问题,但我最近遇到了这种情况。我需要为除最后一项之外的每一项都写出标记。

我在我的用户控件类中创建了一个私有成员变量,并将其设置为绑定到我的转发器的数据源的计数属性并从中减去 1。由于索引是从零开始的,因此索引值与计数相差一个。

私人长项目计数 { 得到;放; }

在 Page_Load 或您调用 DataBind 的任何方法中:

            //Get the count of items in the data source. Subtract 1 for 0 based index.
            itemCount = contacts.Count-1;

            this.repContacts.DataSource = contacts;
            this.repContacts.DataBind();

最后,在您的绑定方法中

           //If the item index is not = to the item count of the datasource - 1

            if (e.Item.ItemIndex != itemCount)
                Do Something....
于 2015-03-28T21:45:37.333 回答
0

当数据绑定发生时,将为转发器设置数据源。因此,在您的 ItemDataBound 事件处理程序中,您可以获取它并根据您的项目索引检查它。这将避免使用 Session 或局部变量。

var datasource = (ICollection)reWorkTags.DataSource;

if (e.Item.ItemIndex + 1 == datasource.Count)
{
    // Do stuff
}
于 2018-08-30T15:17:52.787 回答