Difference with creating and adding controls in PreInit Init
Asked Answered
F

2

10

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,

Fujio answered 21/1, 2011 at 16:34 Comment(0)
E
9

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:

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

}

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:

<%@ 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>...

and you have a skin like this:

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

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

}

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

Erbes answered 21/3, 2011 at 14:1 Comment(0)
P
2

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.

Pentarchy answered 21/1, 2011 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.