Walkthrough: Advanced Use of the ASP.NET Wizard Control
The ASP.NET Wizard control simplifies many of the tasks associated with building forms and collecting user input. In this walkthrough, you use the Wizard control to create a form that collects a user's address information, allows them to opt in or opt out of e-mail updates, and then presents the data and choices back to them in a final confirmation step. Tasks illustrated in this walkthrough include:
Editing the layout of a wizard step.
Adding a custom wizard step.
Programmatically capturing a wizard event.
Branching your wizard based on user choices.
Displaying user data in a completion step.
Prerequisites
In order to complete this walkthrough, you will need:
Microsoft Visual Web Developer.
The .NET Framework.
Creating the Web Site
If you have already created a Web site in Visual Web Developer (for example, by working with the topic Walkthrough: Creating a Basic Web Page in Visual Web Developer), you can use that Web site and skip to "Adding a Wizard Control to the Page" later in this walkthrough. Otherwise, create a new Web site and page by following these steps.
To create a file system Web site
Open Visual Web Developer.
On the File menu, click New Web Site.
The New Web Site dialog box appears.
Under Visual Studio installed templates, click ASP.NET Web Site.
In the Location box, enter the name of the folder where you want to keep the pages of your Web site.
For example, type the folder name C:\WebSites.
In the Language list, click the programming language you prefer to work in.
Click OK.
Visual Web Developer creates the folder and a new page named Default.aspx.
Adding a Wizard Control to the Page
To add a Wizard control to the page
Switch to Design view.
From the Standard group of the Toolbox, drag a Wizard control onto your page.
Collecting Name and Address Information
Next, you configure each step of your wizard to collect the user data, prompt for an e-mail address, and display the collected data in the completion step.
First, collect the user's name, address, city, state, and postal code.
To configure the first step
Drag one of the handles at the edge of the Wizard control to enlarge the control to about twice its default size.
Click the underlined text Step 1 in the Wizard control, and then click in the edit area to the right of Step 1 in the Wizard control.
You can now edit the display area for step 1.
Type Name.
From the Standard group of the Toolbox, drag a TextBox control next to your text.
In the Properties window, change the ID of the TextBox control from TextBox1 to Name.
Repeat the process, adding captions and TextBox controls (remember to name the controls because you will need to refer to them later) to collect:
Address
City
State
PostalCode
Save the file.
Adding an E-mail Update Choice
In this part of the walkthrough, you allow the user to choose whether they would like to receive e-mail updates.
To add an e-mail update choice
Click Step 2.
Click the editable area to the right of Step 2, and type Would you like to receive e-mail updates?.
Beneath the question, type: Yes.
From the Standard group of the Toolbox, drag a RadioButton control next to the word Yes.
Type No.
Drag another RadioButton from the Toolbox onto the control next to the word No.
Adding a Custom Step to Collect an E-mail Address
In this section, you add a custom step to the wizard that collects the user's e-mail address. Later, you will add code to skip this step if the user decides not to get e-mail updates.
Adding a custom step to collect the user's email
Right-click the Wizard control outside of the edit areas and click Show Smart Tag.
In the Wizard Tasks dialog box, choose Add/Remove Wizard Steps.
The WizardStep Collection Editor appears.
In the Add drop-down list on the Add button, click Wizard Step.
Click OK.
The Wizard control displays the new step.
Click the new step to switch it to edit mode.
In the editable area, type E-mail address.
From the Standard group of the Toolbox, drag a TextBox control onto the control next to the words E-mail Address.
In Properties, change the ID of the TextBox control to EmailAddress because you will need to refer to it later in code.
Adding a Completion Step
Now you can add a completion step that will present the user's data back to them.
To add a completion step
Right-click the Wizard control and click Show Smart Tag.
In the Wizard Tasks menu on the control, choose Add/Remove Wizard Steps.
The WizardStep Collection Editor appears.
In the Add list, click Wizard Step.
The Properties area displays the attributes of the new step.
In the Properties window, set StepType to Complete.
Click OK.
The Wizard control adds the new step.
Click the new step on the control, and then click the editable area to the right of the new step.
From the Standard group of the Toolbox, drag a Label control onto the new step.
Press ENTER to create a new line.
Repeat step 7 five more times, for a total of six Label controls corresponding to:
Name
Address
City
Region or State
Postal Code
E-mail Address
Save the file.
Before you can run the wizard, you need to populate the labels in your completion step with the values the user supplied in the first three wizard steps.
Adding Code for the Completion Step
In this walkthrough, the completion step displays the user's data. To ensure the data is displayed, you populate the step's labels programmatically. To display the data, you write code for the page's Load event.
To add code
Switch to Default.aspx and, in Design view, double-click the design surface.
The code editor opens for the page, and creates a skeleton Page_Load method.
Add the following highlighted code to populate the completion step.
Private Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Label1.Text = Name.Text Label2.Text = Address.Text Label3.Text = City.Text Label4.Text = State.Text Label5.Text = PostalCode.Text Label6.Text = EmailAddress.Text End Sub
void Page_Load(object sender, EventArgs e) { Label1.Text = Name.Text; Label2.Text = Address.Text; Label3.Text = City.Text; Label4.Text = State.Text; Label5.Text = PostalCode.Text; Label6.Text = EmailAddress.Text; }
Save the file.
Testing the Wizard Control
To test the Wizard control
Display Default.aspx in Design view.
Open the Wizard Tasks menu on the control and select Step 1 from the Step drop-down list.
Press CTRL+F5 to run the page.
Note
When running the page from Design view, the browser might start on the currently active wizard step, not necessarily the first step. Be sure the wizard's ActiveStepIndex property is set to 0 in the Properties window before launching the page.
Type name and address information.
Click Next.
Select either option for e-mail.
You will write code to handle the e-mail option later in the walkthrough.
Type an e-mail address.
Click Finish.
The data you entered is displayed on the page.
In the next section, you add the code necessary to skip the third step of collecting e-mail if the user chooses not to provide an e-mail address.
Adding Code to Skip the E-mail Step
Now that the data collection and layout portions of your wizard are complete, you will add the logic to allow the user to skip the step that collects the e-mail address if they do not want to receive e-mail updates. You do this by handling the Wizard control's NextButtonClick event. When you handle the event, you first check to see whether the wizard is currently on the step where the user makes a choice. If so, check to see which RadioButton control is selected. If the user has selected No, advance the Wizard control's ActiveStepIndex property to skip the wizard step that requests the user's e-mail address.
To add code to skip the third step
Click the Wizard control. In the Properties window, type Wizard1_NextButtonClick for the NextButtonClick property and press ENTER.
The code editor window opens to the code-behind page.
Add the following highlighted code that checks for the correct step, determines which RadioButton control is selected, and determines whether to skip step 3 of the wizard.
Private Sub Wizard1_NextButtonClick( ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) _ Handles Wizard1.NextButtonClick If Wizard1.ActiveStepIndex = 1 Then If RadioButton1.Checked Then ' Collect e-mail address. Wizard1.ActiveStepIndex = 2 Else Wizard1.ActiveStepIndex = 3 End If End If End Sub
void Wizard1_NextButtonClick(object sender, System.Web.UI.WebControls.WizardNavigationEventArgs e) { if (Wizard1.ActiveStepIndex == 1) { if (RadioButton1.Checked) // Collect ee-mail address. { Wizard1.ActiveStepIndex = 2; } else { Wizard1.ActiveStepIndex = 3; } } }
Setting the ActiveStepIndex property causes the wizard to move directly to the specified step.
Save the file.
Testing the Ability to Skip a Step
To test skipping a wizard step programmatically
Display Default.aspx in Design view.
Open the Wizard Tasks menu on the control and select Step 1 from the Step drop-down list.
Press CTRL+F5 to run the page.
Type name and address information.
Click Next.
In Step 2, select the No check box.
Click Next.
You arrive at the completion step.
Use your browser's back button to return to the first step, or close the browser and press CTRL+F5 to run the page again.
Type name and address information in the first step.
Click Next.
In Step 2, select the Yes check box.
Click Next.
You arrive at the e-mail address collection step.
Type an e-mail address.
Click Next.
You arrive at the completion step.
Next Steps
The Wizard control simplifies the creation of forms to gather user data. In addition to what has been covered here, you might have other questions related to forms as well as to applying a different look and feel to your Wizard control. For example, you might want to:
Override the Wizard control's default text labels. For details, see Wizard Web Server Control Overview
Validate the user's entries in the Wizard control. For details, see Walkthrough: Validating User Input in a Web Forms Page
Learn more about basic Wizard control operation. For details, see Walkthrough: Creating a Basic ASP.NET Wizard Control.