C# 获取数据绑定期间中继器中的项目数
我试图在处理C# 获取数据绑定期间中继器中的项目数,c#,asp.net,.net,collections,web-controls,C#,Asp.net,.net,Collections,Web Controls,我试图在处理OnItemDataBound事件时获取中继器中的项目数。我想要达到的目标非常简单;我试图在中继器中的最后一项上隐藏某个标签。目前,我正在连接到itemsindex和Items.Count,但是在OnItemDataBound期间,索引和计数一起递增 以下是到目前为止我得到的信息: Label myLabel = e.Item.FindControl<Label>("MyLabel"); if (myLabel != null) { // as the item
OnItemDataBound
事件时获取中继器中的项目数。我想要达到的目标非常简单;我试图在中继器中的最后一项上隐藏某个标签。目前,我正在连接到itemsindex
和Items.Count
,但是在OnItemDataBound
期间,索引和计数一起递增
以下是到目前为止我得到的信息:
Label myLabel = e.Item.FindControl<Label>("MyLabel");
if (myLabel != null)
{
// as the item index is zero, I'll need to check against the collection minus 1?
bool isLastItem = e.item.ItemIndex < (((Repeater)sender).Items.Count - 1);
myLabel.Visible = !isLastItem;
}
Label myLabel=e.Item.FindControl(“myLabel”);
如果(myLabel!=null)
{
//由于项目索引为零,我需要检查集合减去1?
bool isLastItem=e.item.item索引<((中继器)发送器).Items.Count-1);
myLabel.Visible=!isLastItem;
}
我知道我可以将
数据源
强制转换为绑定的数据项集合,但是OnItemDataBound
事件处理程序正在多个中继器中使用,因此我需要一些更通用的方法。您可以按照,默认情况下,已将Visible
设置为false:
if (e.Item.ItemIndex > 0)
{
var previousItem = ((Repeater)sender).Items[e.Item.ItemIndex - 1];
var previousLabel = previousItem.FindControl<Label>("MyLabel");
if (previousLabel != null)
{
previousLabel.Visible = true;
}
}
if(e.Item.ItemIndex>0)
{
var-previousItem=((中继器)发送者).Items[e.Item.ItemIndex-1];
var previousLabel=previousItem.FindControl(“MyLabel”);
if(previousLabel!=null)
{
previousLabel.Visible=true;
}
}
我不确定这是否有效-在看到您的代码之前,我不知道您可以访问
repeater.Items
,但这似乎是合理的。如果我试图在第一个项目上隐藏该项目,这将有效,但我想在最后一个项目上隐藏它:)@理查德:是的。。。默认情况下,将Visible
设置为false
,然后此代码将除最后一项外的所有项目设置为true
。(在每一个装订上,它都会找到上一个标签并显示出来。)啊,是的,当然,d'uh。这是一个可能的解决方案,谢谢。