ASP.NET - 在 Page_Pre_init() 或 Page_Init() 或 Page_Load() 中创建的动态控件
在 ASP.NET 中创建动态控件的最佳位置在哪里? MSDN 说 Pre_init ,另一个 MSDN 文章 说 Init,并且 有些人说 Load 事件(我读到这不好做)。
我正在学习 MS 认证,我想确保我知道哪一个是理想的以及为什么。我最初的想法是在 pre_init 中创建对象并在 Load 事件中分配任何属性值(以便为动态控件加载 ViewState)。
Where is the best place to create dynamic controls in ASP.NET? MSDN says Pre_init , another MSDN article says Init, and some people say the Load event (which I read isn't good to do).
I'm studying for a MS certification and I want to make sure I know which one is ideal and why. My initial thought would be to create the object in pre_init and assign any property values in the Load event (so that ViewState would be loaded for the dynamic control).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我推荐Page_Init()。这将绕过 ViewState 加载不一致的问题。控件将可以访问,但视图状态尚未应用于它们。这正是您要根据第二篇文章添加控件的位置。
另外,根据经验,这就是有效的方法。其他任何方法都给我带来了问题。
I recommend Page_Init(). This will bypass the issue of ViewState not loading consistently. Controls will be accessible, but viewstate not yet applied to them. This is exactly where you want to add controls per the second article.
Also, based on experience, this is what works. any other approach else has caused me issues.
这要看情况,但我认为普遍共识是越早越好。因此,如果您要向页面添加动态控件,请尽可能将它们添加到 Pre_Init 阶段。如果要将自定义控件添加到用户控件,请尽可能将它们添加到 Init 阶段(控件没有 Pre_Init)。
在某些情况下,您无法那么早添加它们。
对某些用户输入的响应(例如
按钮单击)。
作为一般经验法则,请尽快添加它们。
It depends but I think the general consensus is the earlier the better. So if you are adding dynamic controls to a Page add them in the Pre_Init phase if you can. If you are adding custom controls to a user control add them in the Init phase if you can (controls don't have a Pre_Init).
There are situations where you can't add them that early.
response to some user input (e.g.
button click).
As a general rule of thumb add them as soon as you can.