区别在preINIT初始化创建和添加控件 [英] Difference with creating and adding controls in PreInit Init

查看:142
本文介绍了区别在preINIT初始化创建和添加控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有是关于ASP.NET的生命周期在网络上吨的信息,但我似乎无法弄清楚时动态控件添加到页面。

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.

在一般有两种情况;与母版的aspx页面,一个没有。这本书我目前正在读(70-515自preP)说要控件添加到页面,而不在preINIT事件处理母版页。要动态控件添加到一个contentpage,我应该把这个逻辑在init事件处理程序。

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.

根据MSDN(http://msdn.microsoft.com/en-us/library/ms178472.aspx)我要创建或在preINIT事件处理程序重新动态控件和控件只读或初始化属性在init事件处理(其中最有意义给我)。周围的Googling我看到很多使用init事件处理程序添加控件的人。

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.

所以,我这里有点失去了 - 什么是正确的方式?并使用preINIT事件处理程序的时候,你怎么能添加控件到您的网页时,所有控件都为空?例如,当你需要动态创建的文本框添加到面板控制?

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?

亲切的问候,

推荐答案

除非你需要玩弄跟踪ViewState中之前控件的属性设置,我会亲自去前进,把我的动态控制除了逻辑在OnInit事件。

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.

如果你真的想preINIT时动态添加控件(使用母版页时),你总是可以做这样的事情:

If you really want to dynamically add a control during the PreInit (when using master page) you can always do something like this:

protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);

    TextBox textBox = new TextBox();
    textBox.Text = "Dynamic TextBox";
    textBox.Width = 100;
    textBox.ReadOnly = false;

    var master = this.Master;

    plcHolder.Controls.Add(textBox);
    textBox.ApplyStyleSheetSkin(this.Page);

}

访问禅师属性将实例化控制
它应该工作,但你得到的嵌套母版页方案(this.Master.Master ...),更新面板等。

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.

这可能是相关和有用的:<一href=\"http://weblogs.asp.net/ysolodkyy/archive/2007/10/09/master-page-and-$p$pinit.aspx\">http://weblogs.asp.net/ysolodkyy/archive/2007/10/09/master-page-and-$p$pinit.aspx

This might be relevant and helpful: http://weblogs.asp.net/ysolodkyy/archive/2007/10/09/master-page-and-preinit.aspx

此外,一个原因,我能想到的(除了以下定义的页面生命周期),MS建议我们把所有在preINIT事件动态控制创建逻辑,所以我们可以利用这个主题服务的,这将自动应用所有可用的皮肤特性对我们来说,Init事件发生之前。

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:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Trace="true" Inherits="_Default" Theme="Test" %>

...

<form id="form1" runat="server">

<div>
<p>
    <asp:TextBox ID="TextBox1" runat="server" TextMode="Password" Text="Control TextBox"></asp:TextBox>
</p>
<p>
    <asp:PlaceHolder ID="plcHolder" runat="server"></asp:PlaceHolder>
</p>

</div>
</form>...

和你有这样的皮肤:

<asp:TextBox runat="server" BackColor="Yellow" Wrap="false" Text="Skin property!" > </asp:TextBox>

只需添加到您的code背后:

Just add this to your code behind:

 private TextBox tb1;
protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);
    tb1 = new TextBox();
    tb1.Text = "PreInit Dynamic TextBox";

    Trace.Write(String.Format("tb1 Wrap Property-> {0}",tb1.Wrap));
    Trace.Write(String.Format("tb1 Text Property-> {0}", tb1.Text));
    Trace.Write("Add tb1 to the placeholder.");
    plcHolder.Controls.Add(tb1);
    Trace.Write(String.Format("tb1 Wrap Property-> {0}", tb1.Wrap));
    Trace.Write(String.Format("tb1 Text Property-> {0}", tb1.Text));
}

protected override void OnInit(EventArgs e)
{
    Trace.Write(String.Format("tb1 Wrap Property-> {0}", tb1.Wrap));
    Trace.Write(String.Format("tb1 Text Property-> {0}", tb1.Text));
    base.OnInit(e);
}



protected void Page_Load(object sender, EventArgs e)
{
    Trace.Write(String.Format("tb1 Wrap Property-> {0}", tb1.Wrap));
    Trace.Write(String.Format("tb1 Text Property-> {0}", tb1.Text));

}

您会发现如何才能进入所有皮肤性质已应用于动态创建的文本框Init事件:)

You will notice how before going into the Init event all skin properties are already applied to the dynamically created textbox :)

这篇关于区别在preINIT初始化创建和添加控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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