Advancing wizard from code-behind in ASP.NET

2020-07-26 18:09发布

问题:

I have a wizard on my page, and it has a "Next" button. I would like to "click" that button from code-behind when clicking another button on the page.

More specifically: There is a button on my page that has two functions: after clicking it, in the postback, it either sets up the code required to reload the page and display a popup, or it advances the wizard if it deems the popup unnecessary. In case the popup is shown, it contains a button to advance the wizard.

Some code snippets:

Wizard init:

<asp:Wizard ID="RegistrationWizard" meta:resourcekey="RegistrationWizard"
    runat="server" OnFinishButtonClick="RegistrationWizard_FinishButtonClick" 
    OnActiveStepChanged="RegistrationWizard_ActiveStepChanged" 
    OnNextButtonClick="RegistrationWizard_NextButtonClick">

Button to show popup or advance:

<asp:Button ID="btnModulesNextPostBack" runat="server" CssClass="submit rounded"
    meta:resourcekey="btnNext" onclick="btnModulesNextPostBack_Click" />

Button to advance wizard:

<asp:Button ID="btnModulesStepNext" runat="server" CssClass="submit rounded" 
    meta:resourcekey="btnNext" CommandName="MoveNext" />

Code-behind of the btnModulesNextPostBack_Click method:

protected void btnModulesNextPostBack_Click(object sender, EventArgs e)
{
    showPopup = false; // if set to true: will open popup in postback
    // ... code to determine if popup should be shown
    if (!showNewsletterPopup)
    {
        // TODO! trigger "move to next step in wizard"
    }
}

I don't know what to place in the TODO line, because I want to be sure that all other methods related to the wizard get called in the same order as usual too (RegistrationWizard_NextButtonClick and RegistrationWizard_ActiveStepChanged and maybe others the wizard code calls internally).

How can I do this? (.net version is 4.0)

回答1:

You should be able to just increment the ActiveStepIndex:

Wizard1.ActiveStepIndex++;

You could also use the MoveTo method:

Wizard1.MoveTo(WizardStep1);

Lastly, if you want to navigate the wizard forward and backwards, you can use the NextButtonClick and PreviousButtonClick:

protected void wzServiceOrder_PreviousButtonClicked(object sender, WizardNavigationEventArgs e)
{
    //decrement the active step index
    wzServiceOrder.ActiveStepIndex--;

    //move the wizard to the active step
    wzServiceOrder.MoveTo(wzServiceOrder.ActiveStep);                        
}

/// <summary>
/// Handles wzServiceOrder OnNextButtonClick event.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void wzServiceOrder_NextButtonClicked(object sender, WizardNavigationEventArgs e)
{
    //increment the active step index
    Wizard1.ActiveStepIndex++;

    //move the wizard to the active step
    Wizard1.MoveTo(Wizard1.ActiveStep);

    if (e.NextStepIndex > Wizard1.WizardSteps.IndexOf(WizardStep1))
    {
        if (!contactsExist)
        {
            Popop1.Show();
            e.Cancel = true;
        }
    }
}


标签: asp.net