java 用户控件_数据绑定和用户控件(Data binding and user controls)

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 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"?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值