How to change button text in ASP.Net wizard control?

While working on ASP.Net Wizard control for a project, I had a requirement about changing text on the buttons that get displayed as user navigates through each step of the wizard. If you use default configuration and settings of Wizard control then you are stuck with button names like Next, Previous and Finish. Sometime is makes sense to put text on these buttons that conveys more clear message about what next or previous step is going to be.

Wizard control provides two ways to customize the buttons in each step.

Declarative Customization Of Wizard Control

You can set up three templates on the page inside the control that control appearance of these buttons. See the following snippet from ASPX page.

<asp:Wizard ID="myWizard" runat="server" >
<FinishNavigationTemplate>
 <asp:Button ID="FinishPreviousButton" runat="server" CausesValidation="False" 
   CommandName="MovePrevious" Text="Previous" />
 <asp:Button ID="FinishButton" runat="server" CommandName="MoveComplete" 
   Text="Finish It" />
</FinishNavigationTemplate>
<StartNavigationTemplate>
   <asp:Button ID="StartNextButton" runat="server" CommandName="MoveNext" 
     Text="Goto Next" />
</StartNavigationTemplate>
<StepNavigationTemplate>
   <asp:Button ID="StepPreviousButton" runat="server" CausesValidation="False" 
     CommandName="MovePrevious" Text="Previous" />
   <asp:Button ID="StepNextButton" runat="server" CommandName="MoveNext" 
     Text="Next" />
</StepNavigationTemplate>
</asp:Wizard>

Codebehind Customization Of Wizard Control

But some time you want to control these button text from codebehind depending on some run time conditions. In that case you can manipulate these step templates from codebehind in any of the events fired. The following code snippet shows how I did it in ActiveStepChanged event that is fired whenever user navigates to a new step.

protected void OnActiveStepChanged(object sender, EventArgs e)
{
  switch (this.myWizard.ActiveStepIndex)
  {
    case 0: // Firt step
      myWizard.StartNextButtonText = "Goto Second Step";
      break;
    case 1: // Second step
      myWizard.StepPreviousButtonText = "Goto First Step";
      myWizard.StepNextButtonText = "Goto Finish Step";
      break;
    case 2: // Finish step
      myWizard.FinishPreviousButtonText = "Goto Step 2";
      myWizard.FinishCompleteButtonText = "Complete It!";
      break;
  }
}

There is one caveat you have to be aware of. From my experience I have seen that if you have declarative templates in ASPX page, the override any changes you make in codebehind.

comments powered by Disqus

Blog Tags