简体   繁体   English

如何在转发器内获取Label而不是itemdatabound

[英]How to get Label inside repeater not in itemdatabound

I'm trying to get a label inside a repeater in a "for" loop, but I keep getting an error saying: 我试图在“for”循环中在转发器中获得一个标签,但我一直收到错误说:

"An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code “mscorlib.dll中出现'System.ArgumentOutOfRangeException'类型的异常,但未在用户代码中处理

Additional information: Index was out of range. 附加信息:指数超出范围。 Must be non-negative and less than the size of the collection." 必须是非负的且小于集合的大小。“

Here's my code: 这是我的代码:

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

You are looping dt.Rows.Count but you are accessing rpOffers.Items . 您正在循环dt.Rows.Count但您正在访问rpOffers.Items It seems the DataTable contains more rows than the repeater. 似乎DataTable包含的行多于转发器。

But why not a simple foreach ? 但为什么不是一个简单的foreach

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

You can use rpOffers.Items.Count instead of dt.Rows.Count . 您可以使用rpOffers.Items.Count而不是dt.Rows.Count

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM