Data Binding and User Control

It really pushes me, so I hope someone can help me a little.


1) Assuming the page contains a user control, then Page_Page_Load starts before UserControl.Page_Load:


a) I assume that if the page contains the ObjectDataSource1 control, then ObjectDataSource1 will perform data binding until UserControl.Page_Load ?!


b) If so, does Page.Prerender occur before UserControl.Page_Load?


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

  • But that doesn’t make much sense, since I assume that the controls inside UserControl follow the life cycle of UserControls, not Pages ?!

  • Anyway, I thought that a web page in which the user control cannot receive events or calls the control methods contained within the user control? If so, how can a web page cause data binding to ObjectDataSource2?


thanks


EDIT:

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

... the country, country and city of the user are read only once from the profile and stored in local variables. UserControl.Page_Load cannot be used for this, because the automatic binding performed using UserControl.ObjectDataSource occurs earlier, so we need to use the UserControl.Page_Init event handler

I assume that in the above quote, the author assumes that if the user control contains ODS, then that ODS will perform data binding until UserControl.Page_Load, what did you not specify?

BTW - the user control mentioned above is added to the web page during development


OTHER EDITING:

I have done several searches, 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

In any case, the quote is taken from page 257, which is mainly part of the section describing the ArticleListing.ascx user control.

BTW - just so you won’t think that Im delusional ... I don’t expect anyone to read the entire section of this user control, I just thought that maybe the code on page 257 would provide enough context to understand what the author really means

+4
source share
1 answer

All your questions are related to the life cycle of an ASP.Net page. You should start here: ASP.Net Page Life Cycle Overview

However, to answer some specific problems.

(1) From the link I cited:

The page raises the OnLoad method event 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 not true. DataBinding occurs immediately before PreRender.

(b) Page.PreRender will happen before UserControl.PageLoad ONLY if UserControl is not added to the page until part of the PreRender of the page life cycle (i.e., is not added dynamically). If so, then all life cycle events for your user control will be triggered immediately after it is added to the "Page Management" collection until it catches its parent container, i.e. Page.

(c) DataBinding will occur at approximately the same time as user control is added to the page at this point. The usercontrol binding sequence will occur after the page controls have been bound to the database.

(c) Labeled dots: Usercontrol has its own life cycle, true, but again, it will not execute until the control is added to the container on the page. This should also answer your second point.


EDIT: This is an interesting excerpt from the book, and I will be tempted to say that it is completely wrong. However, I will need to see what context the author is talking about. Perhaps he is talking about an example object in a book that has special logic in the OnInit handler to make this data binding.

In doing so, I installed a test project to test the default behavior. I added an ObjectDataSource using the Select method, which returns an array of strings, a user control (.ascx) with a relay that binds to the data source, and the 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 also supports this:

The ObjectDataSource control retrieves whenever the Select method is called. This method provides programmatic access to the method that is specified by the SelectMethod property. The specified method, by the SelectMethod property: is automatically called by controls that are bound to the ObjectDataSource when their DataBind method is called. If you set the DataSourceID property data management, the management is automatically associated with data from the data source, if necessary. Customization The DataSourceID property is the recommended binding method. Monitoring ObjectDataSource object data management. Alternatively, you can set the DataSource property, but you must explicitly call the DataBind method for the data control. You can invoke the Select method at any time to retrieve data.

I have to conclude that if this quote is not made in the context of some special circumstance, the author is completely wrong. Maybe he mistakenly wrote "data binding" when he meant "get previously bound values ​​from ViewState"?

+10
source

All Articles