English 中文(简体)
在伙伴关系中增加动态控制。 NET在1.1和2.0之间是否有差别?
原标题:Add dynamic controls in ASP.NET, is there a difference between 1.1 and 2.0?
  • 时间:2009-11-10 14:13:54
  •  标签:

我很肯定会回过来。 http://msdn.microsoft.com/en-us/library/ms1782.aspx” rel=“nofollow noreferer”>Page Life/a>(即_Page_Init/code>)。

Here s one article by Microsoft on it (for .NET 1.0/1.1):
HOW TO: Dynamically Create Controls in ASP.NET:

Note When you create dynamic controls on a Web Form, you must create the controls and add them to the controls collection in either the Page_Init event handler or the Page_Load event handler. Otherwise, the controls may not behave as expected.

However, in a few posts here, it seems like the above is not the case anymore. Controls added within Page_Load seems to be working for everyone else. Some of the posts include:
creating dynamic control in asp.net
Viewstate - utter confusion.

我曾尝试过它,实际上,它工作,尽管我没有做过足够的检验,以排除任何意外的行为。

因此,<代码>Page_Load是增加动态控制的安全阶段? 或者,它只是用于2.0和2.0以上的网络?

最佳回答

我以反省的方式研究了这一问题,而控制阶层确实在你积极补充的情况下加快了步伐,无论在你补充的时候。 它做了一切——负荷观察国家/控制国家、接通后活动、电话活动处理员等。 我不知道在伙伴关系中是否有所不同。 NET 1.x天,但2.0天及以上就是这种情况。

至于“老板”——有些 go子说,经验不足的用户可能走过,因此建议你在英文第1页中添加这些字句。 In。 (请注意,PreInit活动只适用于网页本身,而不是总页或次控制。) 我肯定会再说几句:

  • By default viewstate loads positionally. That is, it ignores control IDs and just takes control placement in the tree into account when loading viewstate. If your dynamic controls were present when the viewstate was serialized, but are not present when it is deserialized, the wrong viewstate item might get assigned to the wrong control, thus leading to exceptions. This can be changed by some settings, though I m now too lazy to search for them.
  • Since the "bringing up to speed" happens when the dynamic control gets added to the page, the order of some events might be unexpected. For example, if you add a TextBox control to the page in the Page_PreRender event, the Changed event of the TextBox will happen there and then. If your event handler code depends on the event happening with the rest of them before PreRender, then you are screwed.
问题回答

你可以在任何时候增加控制。 但是,如果你在上页上加起来,他们只会打碎工作。

事实上,如果你检查了你所贴出的2.Net 2.0版本的生命周期链接,你就会发现这段话是在“Pre Init”活动中引述的:

通过以下活动:...... 建立或重新调整动态控制。

“Load事件”网页是增加控制的一个可接受的场所。 如果你重新读一下你的话,你会注意到你说:

Note: When you create dynamic controls on a Web Form, you must create the controls and add them to the controls collection in either the Page_Init event handler or the Page_Load event handler. Otherwise, the controls may not behave as expected.

如果是协会的话。 NET 2.0篇文章,你在“Catch-up Activities for Added Controls”下,讨论了如何加强控制以加快该网页的速度。





相关问题
热门标签