简体   繁体   English

数据绑定和用户控件

[英]Data binding and user controls

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 : 1)假设页面包含用户控件,则在UserControl.Page_Load之前触发Page.Page_Load


a) I assume that if Page contains ObjectDataSource1 control, then ObjectDataSource1 will perform data binding prior to UserControl.Page_Load ?! a)我假设如果Page包含ObjectDataSource1控件,那么ObjectDataSource1将在UserControl.Page_Load之前执行数据绑定?!


b) If that is the case, then does Page.Prerender also happen prior to UserControl.Page_Load ? b)如果是这种情况,那么Page.Prerender也会在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 )? c)如果UserControl包含ObjectDataSource2控件,该控件是否会在ObjectDataSource1 (直接包含在Page内 )的同时执行数据绑定?

  • But that wouldn't make much sense, since I would assume controls inside UserControl follow UserControl’s life cycle and not Page’s ?! 但这没有多大意义,因为我会假设UserControl中的 控件遵循UserControl的生命周期而不是Page的

  • 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 ? 无论如何,我认为托管用户控件的网页无法接收事件或调用用户控件中包含的控件的方法?!如果是这样,那么网页如何能够在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 UserControl.Page_Load不能用于此,因为UserControl.ObjectDataSource完成的自动绑定发生得更早,所以我们必须使用UserControl.Page_Init事件处理程序

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? 我假设在上面引用作者建议如果用户控件包含ODS,那么这个ODS将在UserControl.Page_Load之前执行数据绑定,这不是你所说的?

BTW - user control the above quote is talking about is added to web page at design time BTW - 用户控制上面提到的引用是在设计时添加到网页


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 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. 无论如何,引用来自第257页,它基本上是描述ArticleListing.ascx用户控件的部分的一部分。

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 顺便说一句 - 这样你就不会认为我是妄想......我不希望任何人阅读关于该用户控件的整个部分,我只是认为第257页的代码可能会提供足够的上下文来弄清楚作者究竟是什么意思

All your questions are related to the ASP.Net page lifecycle. 您的所有问题都与ASP.Net页面生命周期有关。 You should start here: ASP.Net Page Lifecycle Overview 您应该从这里开始: ASP.Net页面生命周期概述

However, to answer some specific concerns. 但是,要回答一些具体问题。

(1) From the link I provided: (1)从我提供的链接:

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. Page调用页面上的OnLoad事件方法,然后递归地为每个子控件执行相同操作,这对每个子控件执行相同操作,直到加载页面和所有控件。

(a) This is incorrect. (a)这是不正确的。 DataBinding occurs just before PreRender. DataBinding恰好在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 (ie added dynamically). (二)将Page.PreRender 在该用户控件不被添加到该页面,直到页的生命周期的预渲染部分(即动态地添加)的情况下UserControl.PageLoad之前发生。 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, ie the Page. 如果是这种情况,那么用户控件的所有生命周期事件将在添加到Page的Control集合后立即连续触发,直到它赶上它的父容器,即Page。

(c) DataBinding will occur about the same time, as long as the usercontrol has been added to the page by this point. (c)DataBinding将在同一时间发生,只要此时将usercontrol添加到页面即可。 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. (c)项目符号点:usercontrol有自己的生命周期,为true,但同样,在将控件添加到页面上的容器之前,它不会被执行。 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. 也许他正在讨论书中的一个示例对象,它在OnInit处理程序中具有特殊逻辑来进行数据绑定。

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. 我添加了一个ObjectDataSource,其中Select方法返回一个字符串数组,一个用户控件(.ascx)带有一个绑定到数据源的转发器,还有一个添加了用户控件的页面。 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: ObjectDataSource文档也支持这个:

The ObjectDataSource control retrieves data whenever the Select method is called. 只要调用Select方法,ObjectDataSource控件就会检索数据。 This method provides programmatic access to the method that is specified by SelectMethod property. 此方法提供对SelectMethod属性指定的方法的编程访问。 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. SelectMethod属性指定的方法由调用其DataBind方法时绑定到ObjectDataSource的控件自动调用。 If you set the DataSourceID property of a data-bound control, the control automatically binds to data from the data source, as needed. 如果设置数据绑定控件的DataSourceID属性,则控件会根据需要自动绑定到数据源中的数据。 Setting the DataSourceID property is the recommended method for binding an ObjectDataSource control to a data-bound control. 设置DataSourceID属性是将ObjectDataSource控件绑定到数据绑定控件的推荐方法。 Alternatively, you can set the DataSource property, but then you must explicitly call the DataBind method of the data-bound control. 或者,您可以设置DataSource属性,但是您必须显式调用数据绑定控件的DataBind方法。 You can call the Select method programmatically at any time to retrieve data. 您可以随时以编程方式调用Select方法来检索数据。

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"? 也许他错误地写了“数据绑定”,当他的意思是“从ViewState中检索以前绑定的值”时?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM