Programming Microsoft ASP.NET 4 - Dino Esposito [215]
The preceding code snippet is an excerpt from a sample wizard that collects input and runs a database query. The first step picks up connection information, whereas the second step lets users define tables, fields, and optionally a WHERE clause. The composed command is shown in the Finish page, where the wizard asks for final approval. (See Figure 9-10.)
Figure 9-10. Two successive pages of the sample wizard: query details and the Finish step.
Canceling Events
The WizardNavigationEventArgs structure also contains a read/write Boolean property named Cancel. If you set this property to true, you just cancel the ongoing transition to the destination page. The following code shows how to prevent the display of the next step if the user is on the Start page and types in john as the user ID:
void OnNext(object sender, WizardNavigationEventArgs e)
{
if (e.CurrentStepIndex == 0 &&
ConnString.Text.IndexOf("UID=john") > -1)
{
e.Cancel = true;
return;
}
}
You can cancel events from within any transition event handler and not just from the NextButtonClick event handler. This trick is useful to block navigation if the server-side validation of the input data has failed. If you do cause a step to fail, though, you’re responsible for showing some feedback to the user.
Note
You can’t cancel navigation from within the ActiveViewChanged event. This event follows any transition events, such as the NextButtonClick or PreviousButtonClick event, and it occurs when the transition has completed. Unlike transition events, the ActiveViewChanged event requires a simpler, parameterless handler—EventHandler.
Finalizing the Wizard
All wizards have some code to execute to finalize the task. If you use the ASP.NET Wizard control, you place this code in the FinishButtonClick event handler. Figure 9-11 shows the final step of a wizard that completed successfully.
void OnFinish(object sender, WizardNavigationEventArgs e)
{
string finalMsg = "The operation completed successfully.";
try
{
// Complete the wizard (compose and run the query)
var command = DetermineCommandText();
var table = ExecuteCommand(ConnString.Text, command);
grid.DataSource = table;
grid.DataBind();
// OK color
FinalMsg.ForeColor = Color.Blue;
}
catch (Exception ex) {
FinalMsg.ForeColor = Color.Red;
finalMsg = String.Format("The operation cannot be completed
due to:
{0}", ex.Message);
}
finally {
FinalMsg.Text = finalMsg;
}
}
string DetermineCommandText()
{
// Generate and return command text here
}
DataTable ExecuteCommand()
{
// Execute database query here
}
Figure 9-11. Final step of a wizard that completed successfully.
If the wizard contains a Complete step, that page should be displayed after the Finish button is clicked and the final task has completed. If something goes wrong with the update, you should either cancel the transition to prevent the Complete page from even appearing or adapt the user interface of the completion page to display an appropriate error message. Which option you choose depends on the expected behavior of the implemented operation. If the wizard’s operation can fail or succeed, you let the wizard complete and display an error message if something went wrong. If the wizard’s operation must complete successfully unless the user quits, you should not make the transition to the Complete page; instead, provide users with feedback on what went wrong and give them a chance to try again.
Summary
Form-based programming is fundamental in Web applications because it’s the only way to have users and applications interact. ASP.NET pages can have only one server-side form with