Question

If I have a label inside an itemtemplate of a repeater, the label is null if I try to access it via repeater.Items[0].FindControl("label"). In fact, Items is 0 even though I have 1 itemtemplate. It isn't until I use the OnItemDataBound event that I can find the control via the e argument. I am curious as to why I need to use the OnItemDataBound event instead of just using repeater.Items[0].FindControl("label"). Can someone explain this?

Was it helpful?

Solution

When you create a template for a repeater you are only telling the repeater what you want to be inserted at execution time - these controls are not initialized in the same way they would be if they were not part of a template.

Controls in the template are created when the repeater is data-bound and due to this fact you will be unable to access them until that point in the repeater's life-cycle.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top