Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [214]

By Root 5545 0
of quick-launch menu for the various steps that form the wizard. You control the sidebar’s visibility through the Boolean DisplaySideBar attribute and define its contents through the SideBarTemplate property.

Regardless of the template, the internal layout of the sidebar is not left entirely to your imagination. In particular, the tag must contain a DataList control with a well-known ID—SideBarList. In addition, the block must contain a button object with the name of SideBarButton. The button object must be any object that implements the IButtonControl interface.

Note

For better graphical results, you might want to use explicit heights and widths for all steps and the sidebar as well. Likewise, the push buttons in the navigation bar might look better if they are made the same size. You do this by setting the Width and Height properties on the NavigationButtonStyle object.

Navigating Through the Wizard


When a button is clicked to move to another step, an event is fired to the hosting page. It’s up to you to decide when and how to perform any critical validation, such as deciding whether or not conditions exist to move to the next step.

In most cases, you’ll want to perform server-side validation only when the user clicks the Finish button to complete the wizard. You can be sure that whatever route the user has taken within the wizard, clicking the Finish button will complete it. Any code you bind to the FinishButtonClick event is executed only once, and only when strictly necessary.

By contrast, any code bound to the Previous or Next button executes when the user moves back or forward. The page posts back on both events.

Filtering Page Navigation with Events


You should perform server-side validation if what the user can do next depends on the data she entered in the previous step. This means that in most cases you just need to write a NextButtonClick event handler:

OnNextButtonClick="OnNext">

...

If the user moves back to a previously visited page, you can usually ignore any data entered in the current step and avoid validation. Because the user is moving back, you can safely assume she is not going to use any fresh data. When a back movement is requested, you can assume that any preconditions needed to visit that previous page are verified. This happens by design if your users take a sequential route.

If the wizard’s sidebar is enabled, users can jump from page to page in any order. If the logic you’re implementing through the wizard requires that preconditions be met before a certain step is reached, you should write a SideBarButtonClick event handler and ensure that the requirements have been met.

A wizard click event requires a WizardNavigationEventHandler delegate (which is defined for you by ASP.NET):

public delegate void WizardNavigationEventHandler(

object sender,

WizardNavigationEventArgs e);

The WizardNavigationEventArgs structure contains two useful properties that inform you about the 0-based indexes of the page being left and the page being displayed. The CurrentStepIndex property returns the index of the last page visited; NextStepIndex returns the index of the next page. Note that both properties are read-only.

The following code shows a sample handler for the Next button. The handler prepares a summary message to show when the user is going to the Finish page:

void OnNext(object sender, WizardNavigationEventArgs e)

{

// Collect the input data if going to the last page

// -1 because of 0-based indexing, add -1 if you have a Complete page

if (e.NextStepIndex == QueryWizard.WizardSteps.Count - 2)

PrepareFinalStep();

}

void PrepareFinalStep()

{

string cmdText = DetermineCommandText();

// Show a Ready-to-go message

var sb = new StringBuilder("");

sb.AppendFormat("You're about to run:

{0}


", cmdText);

sb.Append("
Ready to go?
");

ReadyMsg.Text = sb.ToString();

}

string DetermineCommandText()

{

// Generate and return command text here

}

Each

Return Main Page Previous Page Next Page

®Online Book Reader