添加动态控件在ASP.NET中,有1.1和2.0之间的差异? [英] Add dynamic controls in ASP.NET, is there a difference between 1.1 and 2.0?

查看:149
本文介绍了添加动态控件在ASP.NET中,有1.1和2.0之间的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty确保早在ASP.NET 1.0 / 1.1的日子里,需要在的Page_Load 添加在运行时创建的控件的事件<一个HREF =htt​​p://msdn.microsoft.com/en-us/library/ms178472.aspx相对=nofollow>页面生命周期(即在 Page_Init )。

下面是一文由微软就可以了(用于.NET 1.0 / 1.1):结果
HOW TO:在ASP.NET 动态创建控件:


  

请注意当您创建动态控件
  Web窗体上,你必须创建
  控制,并将它们添加到控件
  集合中的任一Page_Init
  事件处理程序或Page_Load事件
  处理程序。否则,控制可能
  不按预期的行为。


不过,在这里几个帖子,好像上面是不是这样了。控制在的Page_Load 补充似乎是工作的其他人。有些职位包括:结果
<一href=\"http://stackoverflow.com/questions/886664/creating-dynamic-control-in-asp-net\">http://stackoverflow.com/questions/886664/creating-dynamic-control-in-asp-net

<一href=\"http://stackoverflow.com/questions/1708016/viewstate-utter-confusion\">http://stackoverflow.com/questions/1708016/viewstate-utter-confusion

我已经尝试过自己,实际上它的工作,虽然我没有做足够的测试捞出任何异常行为。

那么,的Page_Load 安全阶段添加动态控件?或者是仅适用于上面.NET 2.0和?


解决方案

我曾与反射研究这一点,控制类确实把事情加快速度,当你将它们添加动态,无论你什么时候添加。它所做的一切 - 负载的ViewState /了ControlState-,调用回发事件,调用事件处理程序,等等。我不知道它是在ASP.NET 1.x中日不同,但在2.0及以上这种情况。

至于危险 - 有一些陷阱是没有经验的用户可能会绊倒,所以建议您在Page_Init或之前添加。 (请注意,preINIT事件仅适用于网页本身,而不是母版页或子控件)。把我的头顶部(我敢肯定,有可能是几个):


  • 将默认视图状态的负载位置上。也就是说,它忽略控件ID,只是需要控制位置在树​​上考虑加载视图状态的时候。如果您的动态控制是present当视图状态是序列化的,但不能当它被反序列化present,错误的视图状态的项目可能会被分配到错误的控制,从而导致异常。这可以通过一些设置进行更改,虽然我现在懒得寻找他们。

  • 由于引进来加快速度时发生的动态控制被添加到页面中,某些事件的顺序可能是意想不到的。例如,如果您在Page_ preRender事件TextBox控件添加到页面,TextBox的Changed事件会不会有再发生。如果你的事件处理程序$ C $,c取决于事件与他们的前preRender剩下的发生,那么你就完了。

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:
http://stackoverflow.com/questions/886664/creating-dynamic-control-in-asp-net
http://stackoverflow.com/questions/1708016/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?

解决方案

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.

这篇关于添加动态控件在ASP.NET中,有1.1和2.0之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆