首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取ItemDataBound中的行数

获取ItemDataBound中的行数
EN

Stack Overflow用户
提问于 2011-12-09 20:37:24
回答 4查看 12.5K关注 0票数 1

我有一个绑定到repeaterPage_Load数据源。

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

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

我试过了:

代码语言:javascript
复制
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.

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-12-09 21:11:06

我尽量避免使用Sessions,但最终还是使用了Sessions。

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

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2011-12-09 20:45:37

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

这是因为这些项仍然是绑定的。当绑定时,计数将是当前项索引的+1。

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

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

代码语言:javascript
复制
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(),以显示这应该在绑定中继器之后运行。

票数 4
EN

Stack Overflow用户

发布于 2015-03-29 05:45:37

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

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

私有long itemCount { get;set;}

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

代码语言:javascript
复制
            //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();

最后,在绑定方法中

代码语言:javascript
复制
           //If the item index is not = to the item count of the datasource - 1

            if (e.Item.ItemIndex != itemCount)
                Do Something....
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8445576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档