views:

569

answers:

1

Hello,

This is really bugging me, so I hope someone can help me a bit


1) Assuming page contains user control, then *Page.Page_Load* is fired prior to *UserControl.Page_Load*:


a) I assume that if Page contains ObjectDataSource1 control, then ObjectDataSource1 will perform data binding prior to *UserControl.Page_Load*?!


b) If that is the case, then does Page.Prerender also happen prior to *UserControl.Page_Load*?


c) If UserControl contains ObjectDataSource2 control, will this control perform data binding at the same time as ObjectDataSource1 (which is contained directly inside Page)?

  • But that wouldn’t make much sense, since I would assume controls inside UserControl follow UserControl’s life cycle and not Page’s?!

  • Anyhow, I thought that web page hosting user control can’t receive events or call methods of controls contained inside user control?!If so, then how is web page able to call databind on ObjectDataSource2?


thanx


EDIT:

The main source of my confusion is the following quote from some book:

…user's country, state and city are read only once from the profile and saved in local variables. UserControl.Page_Load cannot be used for this because automatic binding done by the UserControl.ObjectDataSource happens earlier, so we have to use the UserControl.Page_Init event handler

I assume in the above quote author suggests that if user control contains ODS, then this ODS will perform data binding prior to UserControl.Page_Load, which is not what you've stated?

BTW - user control the above quote is talking about is added to web page at design time


ANOTHER EDIT:

I did some googling and the book(or part of it) is available at the following link.

http://books.google.com/books?id=hziE1NB0UkIC&printsec=frontcover&dq=website+programming+asp.net+2.0+design+solution&ei=7lmESv63Npu-ygTO0f2yDg#v=onepage&q=&f=false

Anyways, the quote is taken from page 257, which is basically a part of a section describing ArticleListing.ascx user control.

BTW – just so you won’t think I’m delusional … I don’t expect anyone to read the whole section on that user control, I just thought that perhaps code on page 257 would provide sufficient context to figure out what author actually meant

+6  A: 

All your questions are related to the ASP.Net page lifecycle. You should start here: ASP.Net Page Lifecycle Overview

However, to answer some specific concerns.

(1) From the link I provided:

The Page calls the OnLoad event method on the Page, then recursively does the same for each child control, which does the same for each of its child controls until the page and all controls are loaded.

(a) This is incorrect. DataBinding occurs just before PreRender.

(b) Page.PreRender will occur before UserControl.PageLoad ONLY in the case that the UserControl is not added to the page until the PreRender portion of the Page's lifecycle (i.e. added dynamically). If that is the case, then all the lifecycle events for your user control will be fired in succession immediately after it is added to the Page's Control collection, until it catches up to it's parent container, i.e. the Page.

(c) DataBinding will occur about the same time, as long as the usercontrol has been added to the page by this point. The usercontrol's databinding will occur after the page's controls have been databound.

(c) bulleted points: A usercontrol has its own lifecycle, true, but again, it will not be executed until the control has been added to a container on the page. This should also answer your second bullet point.


EDIT: That's an interesting excerpt from the book, and I would be tempted to say it is entirely incorrect. However, I would need to see what kind of context the author is talking about. Perhaps he's talking about an example object in the book that has special logic in the OnInit handler to do it's databinding.

That being said, I set up a test project just to check default behaviours. I added an ObjectDataSource with a Select method that returns an array of strings, a user control (.ascx) with a repeater that binds to the data source, and a page that added the user control. The order of events was as I expected:

MyObjectDataSource -> Init
UserControl -> Init
Page -> Init
Page -> Load
UserControl -> Load
MyObjectDataSource -> Load
Repeater1 -> DataBinding
MyObjectDataSource -> Selecting
MyObjectDataSource -> SelectMethod
Repeater1 -> DataBound

The ObjectDataSource documentation supports this as well:

The ObjectDataSource control retrieves data whenever the Select method is called. This method provides programmatic access to the method that is specified by SelectMethod property. The method that is specified by the SelectMethod property is called automatically by controls that are bound to the ObjectDataSource when their DataBind method is called. If you set the DataSourceID property of a data-bound control, the control automatically binds to data from the data source, as needed. Setting the DataSourceID property is the recommended method for binding an ObjectDataSource control to a data-bound control. Alternatively, you can set the DataSource property, but then you must explicitly call the DataBind method of the data-bound control. You can call the Select method programmatically at any time to retrieve data.

I am forced to conclude that unless that quote is taken in the context of some special circumstance, that the author is entirely wrong. Maybe he mistakenly wrote "data binding" when he meant "retrieve the previously bound values from ViewState"?

womp
hi,I've updated my post in case you find the time to help me some more
SourceC
Sorry, I won’t bother you again with comments, but just in case you find the time … I’ve made a follow up to your reply. In any case, you’ve already helped me a lot and thank you for that
SourceC
It is available, it just make take some time for it to load up
SourceC