Constructor in web form?

I have an ASP.NET web form where I initialize an array with a list of controls on a page like this

FileUpload[4] = new FileUpload[4];
public myclass()
{
 fileUpload[0] = FileUpload1;
 fileUpload[0] = FileUpload2;
...etc
}

Then I use them when loading the page , and they are all zero. This seems like strange behavior to me. Can someone clarify and explain? I can understand that they are null in the constructor, but why should they be empty when used in page loading.

+3
source share
2 answers

They are null because the controls are not yet created.

Take a look at the ASP.NET Page Life Cycle Overview and the Init Event.

+1
source

"", OnInit(), LoadViewState(), OnInit().

, , , LoadViewState SaveViewState. . ( Visual Studio):

public class MyPage : Page 
{
    class State
    {
        numberOfControls int
        otherState object
    }

    override void LoadViewState(savedState object)
    {
        var myState = (State)savedState;
        SetupMyControls(myState.numberOfControls);
        base.LoadViewState(myState.otherState);
    }

    override object SaveViewState()
    {
        return new State 
        {
            numberOfControls = GetNumberOfMyControls(),
            otherState = base.SaveViewState()
        };
    }
}
0

All Articles