Search code examples
c#asp.netwebformsformwizard

Properties in my code behind all go null when Wizard control's next button is clicked,


I have a wizard control, and I have this code

    protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
    {
        if (Wizard1.ActiveStepIndex == 0)
        {
            if (firstName != null && lastName != null)
            {
                Wizard1.ActiveStepIndex = 1;
            }
            else
            {
                e.Cancel = true;
            }
        }
    }

when the code gets to firstName and lastName they are both null, I have populated them in a previous method above, they are not empty until this event fires. My searching has only let me to something about causes validation, is this the culprit?


Solution

  • Properties will be lost on postback unless you store them in a ViewState or Session or Database.

    e.g. ViewState

      public string Firstname{
    
        get {
    
    return ViewState["Firstname"] == null ?String.Empty :(string)ViewState["Firstname"];
    
        }
        set { ViewState["Firstname"] = value; }
    }