我有一个绑定到repeater的Page_Load数据源。
我用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.
发布于 2011-12-09 21:11:06
我尽量避免使用Sessions,但最终还是使用了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-09 20:45:37
,但是e.Item.ItemIndex和iCount对于每一行都是相同的。
这是因为这些项仍然是绑定的。当绑定时,计数将是当前项索引的+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(),以显示这应该在绑定中继器之后运行。
发布于 2015-03-29 05:45:37
这是一个古老的问题,但我最近确实遇到了这种情况。我需要为除了最后一项之外的每一项都写出标记。
我在我的用户控件类中创建了一个私有成员变量,并将其设置为绑定到我的中继器的数据源的count属性,并从中减去1。由于索引是从零开始的,因此索引值与计数相差1。
私有long itemCount { get;set;}
在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....https://stackoverflow.com/questions/8445576
复制相似问题