与在 PreInit Init 中创建和添加控件的区别
网络上有大量有关 ASP.NET 生命周期的信息,但我似乎无法弄清楚何时向页面动态添加控件。
一般来说有两种情况;一个带母版页的 aspx 页面,一个不带母版页的 aspx 页面。我当前正在阅读的书(70-515 self prep)说在预初始化事件处理程序中向没有母版页的页面添加控件。要动态地将控件添加到内容页,我应该将该逻辑放置在 init 事件处理程序中。
根据MSDN(http://msdn.microsoft.com/en-us/library/ms178472.aspx),我应该在预初始化事件处理程序中创建或重新创建动态控件,并且只读取或初始化初始化事件处理程序中控件的属性(其中对我来说最有意义)。谷歌搜索后我发现很多人使用 init 事件处理程序来添加控件。
所以,我在这里有点迷失了 - 正确的方法是什么?当使用 preinit 事件处理程序时,当所有控件都为空时,如何向页面添加控件?例如,当您需要将动态创建的文本框添加到面板控件时?
亲切的问候,
There's tons of info on the web about the ASP.NET life cycle, but i can't seem to figure out when to dynamically add controls to the page.
In general there are two situations; an aspx page with a masterpage, and one without. The book i'm currently reading (70-515 self prep) says to add controls to a page without a master page in the preinit eventhandler. To dynamically add controls to a contentpage, i should place that logic in the init eventhandler.
According to MSDN (http://msdn.microsoft.com/en-us/library/ms178472.aspx) I should create or recreate dynamic controls in the preinit eventhandler, and only read or initialize properties of controls in the init eventhandler (which makes most sense to me). Googling around I see lots of people using the init eventhandler to add controls.
So, i'm a little bit lost here - what is the correct way? And when using the preinit eventhandler, how could you add controls to your page when all controls are null? For instance, when you need to add a dynamically created textbox to a panel control?
Kind regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非您需要在跟踪 ViewState 之前设置控件属性,否则我个人会继续将动态控件添加逻辑放置在 OnInit 事件中。
如果您确实想在 PreInit 期间(使用母版页时)动态添加控件,您始终可以执行以下操作:
访问“Master”属性将实例化控件
它应该可以工作,但是您会得到嵌套的母版页场景(this.Master.Master ...)、更新面板等。
这可能是相关且有帮助的: http://weblogs.asp .net/ysolodkyy/archive/2007/10/09/master-page-and-preinit.aspx
此外,我能想到的一个原因(除了遵循定义的页面生命周期之外)MS建议我们放置所有逻辑用于在 Preinit 事件中动态创建控件,以便我们可以利用主题服务,该服务将在 Init 事件发生之前自动为我们应用所有可用的皮肤属性。
假设您的标记看起来像这样:
...
并且您有一个像这样的皮肤:
只需将其添加到后面的代码中:
您会注意到在进入 Init 事件之前,所有皮肤属性都已应用于动态创建的文本框:)
Unless you need to play around with setting control properties prior to tracking ViewState, I would personally go ahead and place my dynamic control addition logic in the OnInit event.
If you really want to dynamically add a control during the PreInit (when using master page) you can always do something like this:
accessing the "Master" property would instantiate the controls
and it should work, but you get nested master pages scenarios (this.Master.Master ...), update panels and so on.
This might be relevant and helpful: http://weblogs.asp.net/ysolodkyy/archive/2007/10/09/master-page-and-preinit.aspx
Moreover, one reason I can think of (besides following the defined page lifecycle) MS recommends that we place all the logic for dynamic control creation in the Preinit event so we can take advantage of the theme service, which will apply all available skin properties automatically for us, before the Init event takes place.
Say your markup looks something like that:
...
and you have a skin like this:
Just add this to your code behind:
You will notice how before going into the Init event all skin properties are already applied to the dynamically created textbox :)
PreInit 事件对我来说是新的,但我想它是有意义的,这样您就可以在控件加载和视图状态加载之间有一个中间步骤来完成其他工作。我们使用 init 事件来加载动态控件,这对我们来说一直有效,没有任何问题。我认为您可以接受其中任何一个,但如果 MS 推荐 PreInit,我会说走这条路。这样,在 Init 中,您可以执行任何可能需要的额外工作,并将创建 UI 的例程与可能在视图状态加载之前更新 UI 的例程分开。
HTH。
The PreInit event was new to me, but I guess it makes sense, so that you have an intermediary step in between the loading of controls and viewstate load to do additional work. We've used init event to load dynamic controls and this has always worked for us with no issues. I think you'll be OK with either, but if MS recommends PreInit, I'd say go that route. THis way, in Init, you can do any additional work you may need and separate out the routine that creates the UI vs. the routine that may update it before viewstate load.
HTH.