bind a two-dimensional array to a repeater - error
I have this array:
string[,] productData = new string[5,7];
I bind it to a repeater and a call a method like:
<img src="<%# getPhoto1WithReplace(Container.ItemIndex) %>"
which is defined like:
public String getPhoto1WithReplace(Object itemIndex)
{
int intItemIndex = Int32.Parse(itemIndex.ToString());
if (productData[intItemIndex, 3] != null)
return this.ResolveUrl(productData[intItemIndex, 3].ToString(开发者_开发百科));
else return String.Empty;
}
I do not understand why it calls getPhoto1WithReplace with itemIndex as 5. My array has 5 indexes: 0,1,2,3,4, so HOW Container.ItemIndex can be 5...?
When you bind data to a list-type control such as a repeater, the only thing that it cares about is whether the object that you provided implements the System.Collections.IEnumerable interface.
If you try iterating over a two-dimensional array in a foreach loop, you'll find that you iterate over each item across both dimensions, not just one dimension. I haven't tried binding a two-dimensional array to a repeater, but I would imagine that the repeater is stretching out all of the elements of your array into a flat list.
When I work with repeaters I typically work with the DataItem property of the RepeaterItem (the Container) rather than the ItemIndex, as I usually don't care what the index is. Perhaps that would work for you as well.
You may have to consider revising your data into something more structured than a two-dimensional array.
Do you have a header template for your repeater?
The Items collection is full of RepeaterItems - which doesn't necessarily have to be data items. It can be a header item or a footer item as well.
精彩评论