在ASP.NET中添加动态控件,1.1和2.0有区别吗?

发布于 2024-08-11 05:27:39 字数 932 浏览 11 评论 0 原文

我非常确定在 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬色的秋千 2024-08-18 05:27:39

我已经用 Reflector 研究过这一点,当您动态添加它们时,Control 类确实可以加快速度,无论您何时添加它们。它执行所有操作 - 加载视图状态/控制状态、调用回发事件、调用事件处理程序等。我不知道在 ASP.NET 1.x 时代是否有所不同,但在 2.0 及更高版本中就是这种情况。

至于“危险” - 有一些陷阱,没有经验的用户可能会被绊倒,因此建议您将它们添加到 Page_Init 或之前。 (请注意,PreInit 事件仅适用于页面本身,不适用于母版页或子控件)。在我的脑海中(我确信可能还有更多):

  • 默认情况下,视图状态按位置加载。也就是说,它会忽略控件 ID,而在加载视图状态时仅考虑控件在树中的位置。如果动态控件在视图状态序列化时存在,但在反序列化时不存在,则错误的视图状态项可能会分配给错误的控件,从而导致异常。这可以通过某些设置来更改,尽管我现在懒得搜索它们。
  • 由于“加快速度”是在将动态控件添加到页面时发生的,因此某些事件的顺序可能是意外的。例如,如果在 Page_PreRender 事件中向页面添加一个 TextBox 控件,则该 TextBox 的 Changed 事件就会立即发生。如果您的事件处理程序代码依赖于 PreRender 之前其余部分发生的事件,那么您就完蛋了。

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):

  • 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.
南风几经秋 2024-08-18 05:27:39

您可以随时添加控件。但是,如果您在页面加载之前添加它们,它们只能与视图状态一起使用。

事实上,如果您检查您发布的页面生命周期链接的 .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:

Use this event for the following: ... Create or re-create dynamic controls.

隔纱相望 2024-08-18 05:27:39

Page_Load 事件处理程序是添加控件的可接受位置。如果您重新阅读您的笔记,您会注意到他们是这么说的。

注意:创建动态控件时
在 Web 表单上,您必须创建
控件并将它们添加到控件中
Page_Init 中的集合
事件处理程序或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.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文