数据绑定和用户控件
这真的很困扰我,所以我希望有人能帮助我一点
1)假设页面包含用户控件,则Page.Page_Load在UserControl.Page_Load之前触发>:
a) 我假设如果 Page 包含 ObjectDataSource1 控件,那么 ObjectDataSource1 将在 UserControl.Page_Load 之前执行数据绑定?!
b) 如果是这种情况,那么 Page.Prerender 是否也会先于 UserControl.Page_Load 发生?
c) 如果UserControl包含ObjectDataSource2控件,该控件是否会与ObjectDataSource1(直接包含在内部)同时执行数据绑定页)?
但这没有多大意义,因为我假设UserControl内的控件遵循UserControl的生命周期,而不是Page的?!< /p>
无论如何,我认为托管用户控件的网页无法接收事件或调用用户控件内包含的控件的方法?!如果是这样,那么网页如何能够在ObjectDataSource2上调用数据绑定?
thanx
编辑:
我困惑的主要根源是以下来自某本书的引用:
…用户的国家、州和城市仅从配置文件中读取一次并保存在本地变量中。 UserControl.Page_Load 不能用于此目的,因为 UserControl.ObjectDataSource 完成的自动绑定发生较早,因此我们必须使用 UserControl.Page_Init 事件处理程序
我假设在上面的引用中作者建议如果用户控件包含 ODS,那么此 ODS 将在 UserControl.Page_Load 之前执行数据绑定,这不是您所说的?
顺便说一句 - 上面引用的用户控件在设计时添加到网页
另一个编辑:
我做了一些谷歌搜索,这本书(或其中的一部分)可以在以下链接中找到。
无论如何,引用来自第 257 页,这基本上是描述<的部分的一部分em>ArticleListing.ascx 用户控件。
顺便说一句 - 只是这样你就不会认为我有妄想……我不希望任何人阅读有关该用户控件的整个部分,我只是认为第 257 页上的代码可能会提供足够的上下文来弄清楚作者的实际含义
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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的所有问题都与 ASP.Net 页面生命周期相关。 你应该从这里开始:
ASP.Net 页面生命周期概述
但是,要回答一些特定的问题。
(1)从我提供的链接:
(a) 这是不正确的。 数据绑定发生在 PreRender 之前。
(b) Page.PreRender 将在 UserControl.PageLoad 之前发生 ONLY 如果 UserControl 没有添加到页面,直到页面生命周期的 PreRender 部分(即添加动态)。 如果是这种情况,则用户控件的所有生命周期事件将在添加到 Page 的 Control 集合后立即连续触发,直到它赶上其父容器(即 Page)。
(c) 只要用户控件此时已添加到页面,数据绑定就会大约在同一时间发生。 用户控件的数据绑定将在页面控件进行数据绑定后发生。
(c) 要点:用户控件有自己的生命周期,确实如此,但同样,只有在将控件添加到页面上的容器中时,它才会执行。 这也应该回答你的第二个要点。
编辑:这是书中有趣的摘录,我很想说它是完全错误的。 不过,我需要看看作者在谈论什么样的背景。 也许他正在谈论书中的一个示例对象,该对象在 OnInit 处理程序中具有特殊的逻辑来执行数据绑定。
话虽这么说,我设置了一个测试项目只是为了检查默认行为。 我添加了一个带有返回字符串数组的 Select 方法的 ObjectDataSource、一个带有绑定到数据源的转发器的用户控件 (.ascx),以及一个添加了用户控件的页面。 事件的顺序如我所料:
ObjectDataSource文档也支持这一点:
我不得不得出这样的结论:除非这句话是在某些特殊情况下引用的,否则作者是完全错误的。 也许他错误地写了“数据绑定”,而他的意思是“从 ViewState 检索先前绑定的值”?
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:
(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:
The ObjectDataSource documentation supports this as well:
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"?