Xaisoft
Xaisoft

Reputation: 46591

Why is a label only accessible through the repeaters OnItemDataBound event?

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?

Upvotes: 0

Views: 212

Answers (1)

Andrew Hare
Andrew Hare

Reputation: 351476

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.

Upvotes: 1

Related Questions