.net.

Ok, this is a very annoying error that I had problems with everything in the morning!

I have a custom control that we have used in many projects that have properties set and stored in the ViewState by the onload calling pages. the control sets up child controls by using the properties of the custom control's CreateChildControls () method.

Typically, as usual, the callback fires the Page_Load event, then fires the CreateChildControls method of the control on the page.

Strange tone, although we have a login system (custom membership provider) on the site and when the user logs in otherwise, the CreateChildControls () method first fires the Page_Load object, so the control properties are incorrect (set from previous feedback)

How can events fire in a different order? I thought all page events happened in the same order no matter what and I can't see how the login would change that order.

UPDATE: The problem seems to be that I am not calling EnsureChildControls (), but I'm not sure where to call it? If a control has multiple properties set that are used when setting up child controls, when should I call EnsureChildControls (), I think I am confused about what EnsureChildControls () does?

0


a source to share


2 answers


CreateChildControls is called whenever an ASP.NET page needs them. There is no specific point in the page cycle for this. This can happen in the Init event, it can happen in the Load event. If you want your child controls to be available, call the EnsureChildControls () method of your control. You can do this in the Init event for a control to make sure you have child controls throughout its lifecycle, or you can do this every time you need a reference to one of the child controls - for example. in the getter / setter properties of your control.



+4


a source


When creating the management server / user properties that need access to the contained child controls, I use the following:

public Whatever SomeProperty
{

    get
    {
        EnsureChildControls();
        <more code here>
    }
    set
    {
        EnsureChildControls();
        <more code here>
    }
}

      



This ensures that your governing consumers are free to work with your management through different stages of the page lifecycle.

+1


a source







All Articles