Property null after postback - Dynamically loaded control
Asked Answered
C

3

6

I'm aware this question has been asked many times before but I suspect I have a unique scenario.

I'm loading a Child Control (ASCX) and setting a Property on that Control. This works perfectly fine until postback where the property is null.

Herewith the First Class which loads the ChildControl :

protected override void CreateChildControls()
{
    MyUserControl control = (MyUserControl)Page.LoadControl(_ascxPath);
    control.MyProperty = base.MyProperty
    Controls.Add(control);
}

Then, on my Child Control I've got the following code:

public partial class MyUserControl : UserControl
{
    public MyType MyProperty { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        //Exception on next line because Property is null (only on postback)
        var somevalue = MyProperty.SubProperty;
Chilcote answered 7/6, 2012 at 8:45 Comment(2)
Where is CreateChildControls called from? Is it always created regardless of what IsPostBack is?Meggie
It is always called. This is a Sharepoint Solution. The CreateChildControls() event/method is within a class which inherits from Microsoft.SharePoint.WebPartPages.WebPart. I doubt though if the issue is Sharepoint related.Chilcote
F
12

Ok. Let me try to explain it.
1. Once page is created, you get full page lifecycle
2. You click on some control to create user control, and you get it
3. Now you are entering value to this control, and getting postback
4. On server side postback is handled, but as you can see viewstate actions appear as soon as page is loaded.
One of main purposes of viewstate is handling control events, to see if they are changed, or save their states or something else.
5. If on the moment, when viewstate is loaded you control is still not constructed, then all it's events and values would be ignored.

Solution either make it static control and just hide it, either create it before viewstate actions started.

Fillip answered 7/6, 2012 at 9:25 Comment(3)
Tx for advice. Will response back shortly after changing code.Chilcote
Thank you. Your advice worked. I moved the Property Initialising code to the OnInit Event and now everything worked fine.Chilcote
Your welcome, I've spend a lot of time on this issue when faced it myself.Fillip
L
6

You need to add the control and set properties in the Page_Init event, other wise you will lose the properties value.

Lacy answered 7/6, 2012 at 9:57 Comment(3)
Tx, busy working on that. Will get back shortly with the resultsChilcote
Thanks Imran. Your advice also helped. Unfortunately I cannot mark two answers as correct, but +1 to you. TxChilcote
No worries dear, glad that you got answer, doesn't matter who gave. CheersLacy
R
2

In Microsoft explanations about ASP.NET page life cycle, it is written that dynamically created controls must be created in PreInit.

It worked for me. Here is my main page :

protected global::System.Web.UI.HtmlControls.HtmlGenericControl FiltersZone;

(. . .)

 protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);
        FiltersZone.Controls.Add(new PlanningFiltersSurgeonWeb());
    }

This dynamically created ".ascx" control contains an hidden field :

<input id="hidTxtPaint" type="hidden" name="hidTxtPaint" runat="server" />

I am now able to retrieve its value from within dynamically created ASCX control Page_Load event, after a "submit" or a "__dopostback('hidTxtPaint')" initiated from JavaScript.

On the other hand, the hidden field's value is always empty after a POST if its parent ".ascx" control is added in main page's Page_Load event.

Roxy answered 16/2, 2016 at 9:18 Comment(1)
A comprehensive explanation that's more correct and helpful than the accepted answer - Thanks!Kittrell

© 2022 - 2024 — McMap. All rights reserved.