如何在中继器内获取标签而不是在 itemdatabound 中

How to get Label inside repeater not in itemdatabound

我试图在 "for" 循环中的中继器内获取标签,但我一直收到错误提示:

"An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code

Additional information: Index was out of range. Must be non-negative and less than the size of the collection."

这是我的代码:

for (var i = 0; i < dt.Rows.Count; i++)
{
    Label AppAmmount = (Label)rpOffers.Items[i].FindControl("AppAmmount");
}

您正在循环 dt.Rows.Count 但您正在访问 rpOffers.ItemsDataTable 似乎比转发器包含更多行。

但为什么不是简单的 foreach

foreach(RepeaterItem item in rpOffers.Items)
{
    Label AppAmmount = (Label)item.FindControl("AppAmmount");
}

您可以使用 rpOffers.Items.Count 而不是 dt.Rows.Count

for (var i = 0; i < rpOffers.Items.Count; i++)
{
    Label AppAmmount = (Label)rpOffers.Items[i].FindControl("AppAmmount");
}