gpt4 book ai didi

c# - 在 OnItemDataBound 期间获取中继器中的项目数

转载 作者:行者123 更新时间:2023-11-30 17:12:19 26 4
gpt4 key购买 nike

我试图在处理 OnItemDataBound 事件时获取中继器中的项目数。我想要实现的目标非常简单;我试图在中继器的最后一项隐藏某个标签。目前我正在连接到 ItemIndexItems.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;
}

我知道我可以将 DataSource 转换为绑定(bind)的数据项集合,但是 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;
}
}

确定这是否可行 - 在我看到你的代码之前我不知道你可以访问 repeater.Items - 但它似乎是合理的。

关于c# - 在 OnItemDataBound 期间获取中继器中的项目数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10782277/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com