我非常确定在 ASP.NET 1.0/1.1 时代,运行时创建的控件需要在 Page_Load 事件之前添加/en-us/library/ms178472.aspx" rel="nofollow noreferrer">页面生命周期(即在 Page_Init
内)。
这是 Microsoft 撰写的一篇关于它的文章(针对 .NET 1.0/1.1):
如何:在 ASP.NET 中动态创建控件:
创建动态控件时的注意事项
在 Web 表单上,您必须创建
控件并将它们添加到控件中
Page_Init 中的集合
事件处理程序或 Page_Load 事件
处理程序。否则,控件可能会
行为不符合预期。
然而,在这里的一些帖子中,似乎上述情况不再是这样了。 Page_Load
中添加的控件似乎对其他人都有效。一些帖子包括:
在 asp.net 中创建动态控件
Viewstate - 完全混乱。
我自己尝试过,确实有效,尽管我做得还不够进行测试以发现任何意外行为。
那么 Page_Load
是添加动态控件的安全阶段吗?或者仅适用于.NET 2.0 及更高版本?
I am pretty sure back in the days of ASP.NET 1.0/1.1, controls created during runtime needs to be added before Page_Load
event of the Page Lifecycle (i.e. inside Page_Init
).
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.
I've tried it myself and indeed it worked though I've not done enough test to fish out any unexpected behavior.
So is Page_Load
a safe stage to add dynamic controls? Or is it only for .NET 2.0 and above?
发布评论
评论(3)
我已经用 Reflector 研究过这一点,当您动态添加它们时,Control 类确实可以加快速度,无论您何时添加它们。它执行所有操作 - 加载视图状态/控制状态、调用回发事件、调用事件处理程序等。我不知道在 ASP.NET 1.x 时代是否有所不同,但在 2.0 及更高版本中就是这种情况。
至于“危险” - 有一些陷阱,没有经验的用户可能会被绊倒,因此建议您将它们添加到 Page_Init 或之前。 (请注意,PreInit 事件仅适用于页面本身,不适用于母版页或子控件)。在我的脑海中(我确信可能还有更多):
I have studied this with Reflector, and the Control class does indeed bring things up to speed when you add them dynamically, no matter when you add them. It does everything - loads viewstate/controlstate, calls postback events, calls event handlers, etc. I don't know if it was different in ASP.NET 1.x days, but in 2.0 and above this is the case.
As for the "dangers" - there are some gotchas that the inexperienced user might trip over, so it is recommended that you add them in Page_Init or before. (Note that the PreInit event only applies to the page itself, not the Master Page or subcontrols). Off the top of my head (I'm sure there might be a few more):
您可以随时添加控件。但是,如果您在页面加载之前添加它们,它们只能与视图状态一起使用。
事实上,如果您检查您发布的页面生命周期链接的 .Net 2.0 版本,您仍然会在 PreInit 事件下找到以下引用:
You can add controls at any time. However, they'll only work with viewstate if you add them before page loads.
In fact, if you check the .Net 2.0 version of the page lifecycle link you posted, you'll stilll find this quote under the PreInit event:
Page_Load 事件处理程序是添加控件的可接受位置。如果您重新阅读您的笔记,您会注意到他们是这么说的。
如果您链接到的 ASP.NET 2.0 文章,在“添加的控件的追赶事件”下,他们讨论了如何使添加的控件与页面保持同步。
The Page_Load event handler is an acceptable place to add controls. If you re-read your note you will notice that they state that.
If the ASP.NET 2.0 article you linked to, under "Catch-up Events for Added Controls", they discuss how added controls are brought up to speed with the page.