views:

113

answers:

1

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?

+1  A: 

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.

Andrew Hare
This explains why if I do a <# Eval("...") inside a repeater, it works ok, but if I try to access a label.text property, it fails and I have to use the OnItemDatabound event.
Xaisoft
Did I get the above comment correct? Thanks for your help.
Xaisoft
@Xaisoft - Yes, you are correct!
Andrew Hare