Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取数据绑定期间中继器中的项目数_C#_Asp.net_.net_Collections_Web Controls - Fatal编程技术网

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。这是一个可能的解决方案,谢谢。