ASP.NET - 在Page_Pre_init()或Page_Init()或Page_Load()中创建的动态控件 [英] ASP.NET - Dynamic controls created in Page_Pre_init() or Page_Init() or Page_Load()

查看:186
本文介绍了ASP.NET - 在Page_Pre_init()或Page_Init()或Page_Load()中创建的动态控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET中创建动态控件的最佳位置在哪里? MSDN 说Pre_init,另一个 MSDN文章说Init,而一些人说负载事件(我读的不好做)。



我正在学习MS认证,我想确保我知道哪一个是理想的,为什么。我最初的想法是在pre_init中创建对象,并在Load事件中分配任何属性值(以便为动态控件加载ViewState)。

解决方案

这取决于,但我认为普遍的共识是越早越好。所以如果你添加一个页面的动态控件添加它们在Pre_Init阶段,如果可以的话。如果您将自定义控件添加到用户控件,则可以在Init阶段添加它们(如果控件没有Pre_Init)。



有些情况下您无法尽早添加。



  1. 响应中添加一个控件到一些用户输入(例如
    按钮单击)。

  2. 您需要根据您所在页面的状态加载特定的控件。在这种情况下,您可能需要等到包含控件的加载事件来确定是否需要加载控件。

作为一般的经验法则可以尽快添加它们。


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

解决方案

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.

  1. Adding a control in response to some user input (e.g. button click).
  2. You need to load a specific control based on the state of the page you are on. In this case you'll probably have to wait until the load event of the containing control to determine if you need to load the control or not.

As a general rule of thumb add them as soon as you can.

这篇关于ASP.NET - 在Page_Pre_init()或Page_Init()或Page_Load()中创建的动态控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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