10

I have a repeater control on an ASP.NET 2.0 web form.

As I understanding it, all of the page's data-bound controls fire their binding events somewhere in between the Page_Load and the Page_PreRender events.

However, my repeater's ItemDataBound event appears to be happening AFTER the PreRender event.

How is this so and is there any way I can access the page controls AFTER all the ItemDataBound events have fired?

Updates:

  • The Repeater uses an ObjectDataSource with the DataSourceID declarative set in the repeater control.

  • The DataSource ID or object is not modified at all during the page life-cycle.

bentsai
  • 3,063
  • 1
  • 27
  • 29
Matias Nino
  • 4,265
  • 13
  • 46
  • 63

3 Answers3

7

Declarative databinding (datasource specified via the DataSourceID property) occurs later than the PreRender event. The behavior you are observing is by design. If this is not what you need you should explicitly databind your control - just call its DataBind method.

Atanas Korchev
  • 30,562
  • 8
  • 59
  • 93
  • 1
    If that is true than how do you explain this: http://msdn.microsoft.com/en-us/library/ms178472.aspx PreRender | Before this event occurs: Each data bound control whose DataSourceID property is set calls its DataBind method. – Matias Nino Oct 21 '08 at 00:35
  • 3
    I tested with a simple page. Here is the order in which the events were executed: Page_Load Page_PreRender Repeater1_DataBinding Repeater1_ItemDataBound Repeater1_PreRender According to Reflector the Repeater.EnsureDataBound is called in Repeater.OnPreRender. – Atanas Korchev Oct 21 '08 at 18:41
  • 1
    You are correct! Thanks! Looks like the repeater is not your average data-bound control! – Matias Nino Oct 22 '08 at 15:17
1

Are you specifically binding your repeater (myRepeater.DataBind();) in your code behind file (for example inside the Page_Load() event)?

Have you checked out the ASP.NET events lifecycle? Sorry if you already know this, but just in case: http://msdn.microsoft.com/en-us/library/ms178472.aspx

Hope it helps.

Ricardo.

Ricardo Villamil
  • 5,031
  • 2
  • 30
  • 26
  • Nope. The repeater's databind is not explicitly called. That's why I figured it would occur after the page Load but before the PreRender, as stated by the page lifecycle. – Matias Nino Oct 20 '08 at 20:06
0

I think I had a similar situation, and my option was to FORCE the controls to bind themselves, by calling EnsureChildControls or some similar method.

hova
  • 2,811
  • 20
  • 19
  • I know I can force it to databind during Page_Load, but I was just curious as to WHY it was waiting until after the Page_PreRender to bind. Wierd! – Matias Nino Oct 20 '08 at 19:28