ASP.NET - 在PAGE_ pre_init()或Page_Init()或Page_Load中创建动态控件()控件、动态、NET、ASP

2023-09-05 00:11:27 作者:末唧

在哪里是在ASP.NET创建动态控件的最佳地点? MSDN 说:pre_init,另外的 MSDN文章初始化说,和的有人说Load事件(这是我读的是不是好做的)。

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

我攻读MS认证,我想确保我知道哪一个是理想的原因。我最初的想法是创建于pre_init对象,并指定Load事件中的任何属性值(这样的ViewState将用于动态控制加载)。

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

推荐答案

这要看,但我认为,普遍的共识是越早越好。因此,如果您要添加动态控件到页面,如果你可以将它们添加在pre_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.

添加在控制 响应某些用户输入(例如 点击按钮)。 您需要加载基于你所在的页面状态的特定控制。在这种情况下,你可能要等到包含控件的加载事件,以确定是否需要加载的控制与否。 Adding a control in response to some user input (e.g. button click). 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.