简体   繁体   中英

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:

"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."

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 . It seems the DataTable contains more rows than the repeater.

But why not a simple foreach ?

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

You can use rpOffers.Items.Count instead of dt.Rows.Count .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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