How do I navigate to a specific step of a Wizard control in ASP.NET

5.8k Views Asked by At

I have a Wizard control in an ASP.NET page. I am trying to provide links back to the individual WizardSteps. I can't find any information about doing it.

Here is some code to help visualize my problem

<asp:Wizard runat="server" ID="Wizard">
    <HeaderTemplate>
        <ul>
            <li><a href="<%=LinkToTheFirstStep %>">Step 1</a></li>
            <li><a href="<%=LinkToTheSecondStep %>">Step 2</a></li>
        </ul>
    </HeaderTemplate>
    <WizardSteps>
        <WizardSteps runat="server" ID="WizardStepPersonal">
        </WizardSteps>
        <WizardSteps runat="server" ID="WizardStepTravel">
        </WizardSteps>
    </WizardSteps>
</asp:Wizard>
2

There are 2 best solutions below

0
Keenan On

You could just have a query string variable, like ?step=1 and check against that on page load, then set your wizard like:

Wizard.ActiveStepIndex = int.Parse(Request["step"]);

Of course with a little more error checking.

Edit: Sorry, the ActiveStepIndex accepts and int and I initially was trying to cast a string to to an int.

0
Filburt On

You could use an asp:Menu

The page

<asp:Menu ID="MyWizardMenu" Orientation="Horizontal" OnMenuItemClick="MyWizardMenu_MenuItemClick" runat="server">
    <Items>
        <asp:MenuItem Text="One" Selected="true" Value="0"></asp:MenuItem>
        <asp:MenuItem Text="Two" Value="1" ></asp:MenuItem>
        <asp:MenuItem Text="Three" Value="2"></asp:MenuItem>
    </Items>
</asp:Menu>

<asp:Wizard ID="MyWizard" runat="server" OnNextButtonClick="MyWizard_NextButtonClick"  OnPreviousButtonClick="MyWizard_PreviousButtonClick" DisplaySideBar="false">
    <WizardSteps>
        <asp:WizardStep ID="one" Title="One" runat="server">One</asp:WizardStep>
        <asp:WizardStep ID="two" Title="Two" runat="server">Two</asp:WizardStep>
        <asp:WizardStep ID="three" Title="Three" runat="server">Three</asp:WizardStep>
    </WizardSteps>
</asp:Wizard>

The code behind

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        MyWizard.ActiveStepIndex = 0;
    }
}

protected void MyWizardMenu_MenuItemClick(object sender, MenuEventArgs e)
{
    int index = Int32.Parse(e.Item.Value);
    MyWizard.ActiveStepIndex = index;
}

protected void MyWizard_NextButtonClick(object sender, WizardNavigationEventArgs e)
{
    MyWizardMenu.Items[e.NextStepIndex].Selected = true;
}

protected void MyWizard_PreviousButtonClick(object sender, WizardNavigationEventArgs e)
{
   MyWizardMenu.Items[e.CurrentStepIndex - 1].Selected = true;
}

It should even be possible to bind MyWizard.WizardSteps to MyWizardMenu.DataSource (to avoid hardcoding your steps to navigation items) but I didn't get that to work yet because simply casting to IHierachicalEnumaration is not enough.