I enjoy working with the ASP.NET Wizard control – it’s one of the more useful controls available, and there always seems to be a place for a wizard in the custom apps I’ve written. Over time I’ve developed a helper method that makes it easier to do non-linear jumps from step to step.
GetIndexFromStep(WizardStepBase step)
Often, in a NextButtonClick event handler for a Wizard, you need to figure out what the current step is that you are on, based on the WizardNavigationEventArgs e.CurrentStepIndex property. Here is a helper method to enable you to do that:
1 |
<span style="color: #008000">/// <summary></span> |
1 |
<span style="color: #008000">/// Returns the index of a particular wizard step in the WizardSteps collection</span> |
1 |
<span style="color: #008000">/// </summary></span> |
1 |
<span style="color: #008000">/// <param name="step">The step whose index number you want</param></span> |
1 |
<span style="color: #008000">/// <returns></returns></span> |
1 |
<span style="color: #0000ff">private</span> <span style="color: #0000ff">int</span> GetIndexFromStep(WizardStepBase step) |
1 |
{ |
1 |
<span style="color: #0000ff">return</span> step.Wizard.WizardSteps.IndexOf(step); |
1 |
} |
You can also package it up as an extension method (.NET 3.5) off of the Wizard or WizardStepBase object:
1 |
<span style="color: #0000ff">using</span> System.Web.UI.WebControls; |
1 |
  |
1 |
<span style="color: #008000">/// <summary></span> |
1 |
<span style="color: #008000">/// Extensions for ASP.NET Wizard Control Objects</span> |
1 |
<span style="color: #008000">/// </summary></span> |
1 |
<span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">class</span> WizardExtensions |
1 |
{ |
1 |
1 |
<span style="color: #008000">/// <summary></span> |
1 |
<span style="color: #008000">/// Gets the index of a particular wizard step.</span> |
1 |
<span style="color: #008000">/// </summary></span> |
1 |
<span style="color: #008000">/// <param name="wizard">The current Wizard</param></span> |
1 |
<span style="color: #008000">/// <param name="step">The step whose index number you want</param></span> |
1 |
<span style="color: #008000">/// <returns></returns></span> |
1 |
<span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> GetIndexFromStep(<span style="color: #0000ff">this</span> Wizard wizard, WizardStepBase step) |
1 |
{ |
1 |
<span style="color: #0000ff">return</span> wizard.WizardSteps.IndexOf(step); |
1 |
} |
1 |
<span style="color: #008000">/// <summary></span> |
1 |
<span style="color: #008000">/// Gets the index of this wizard step.</span> |
1 |
<span style="color: #008000">/// </summary></span> |
1 |
<span style="color: #008000">/// <param name="step">This step</param></span> |
1 |
<span style="color: #008000">/// <returns></returns></span> |
1 |
<span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> GetIndex(<span style="color: #0000ff">this</span> WizardStepBase step) |
1 |
{ |
1 |
<span style="color: #0000ff">return</span> step.Wizard.WizardSteps.IndexOf(step); |
1 |
} |
1 |
} |
If you’ve given your wizard steps specific IDs, then you can use them strongly typed in a NextButtonClick event:
1 |
<span style="color: #0000ff">protected</span> <span style="color: #0000ff">void</span> Wizard1_NextButtonClick(<span style="color: #0000ff">object</span> sender, WizardNavigationEventArgs e) |
1 |
{ |
1 |
<span style="color: #0000ff">if</span> (e.CurrentStepIndex == GetIndexFromStep(step1)) |
1 |
{ |
1 |
<span style="color: #008000">// We are on step #1</span> |
1 |
  |
1 |
<span style="color: #008000">// Validate...</span> |
1 |
  |
1 |
<span style="color: #008000">// Skip to step 3</span> |
1 |
Wizard1.ActiveStepIndex = GetIndexFromStep(step3); |
1 |
  |
1 |
} |
1 |
} |