C#: Wizard Server Control
By: Zack Turnbow
Introduction
Typically there may come a time when there is a need to have the user fill out information in a series or sequence of steps. When this need is apparent, often it requires multiple pages or post packs to accomplish what is needed. Along comes the Wizard server control that handles this exact requirement. This control was released in the ASP.NET 2.0 framework and has been built upon in the 3.5 framework incorporating AJAX capabilities. This article will cover a simple use for the Wizard control.
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
Implementation
To get started, create a new project in Visual Studio.
[Click to see full-size]
Once the project opens, go to the design screen. Locate the wizard control in the tool box and drag it on to the page.
Go ahead and give it a width of 100% in the properties window so that there will be plenty of room to add controls to each step of the wizard. By default, the wizard comes with 2 steps, so to show how easy it is to add new steps click on the wizard control and then upper right corner click on the > arrow button to see option configuration properties. In the list shown, select Add/Remove Wizard Steps. A new window will open up to give the ability to add or remove wizard steps.
[Click to see full-size]
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
Click the Add button and give it the generic name of Step 3 then click the OK button. On the design page, the newly added step will appear.
[Click to see full-size]
Now click on Step 1 and in the highlight box above the Next button type: Please enter your name: Then drag a textbox out to the right of the typed text.
[Click to see full-size]
Click on the Step 2 link and the design area will be blank for the next step. Again in the highlighted area type the following: Please enter your nationality: Also, drag another textbox to the right of the typed text.
[Click to see full-size]
Click on the Step 3 link and again the design area will be a blank canvas to add the controls needed for this step. To keep this uniform, add the following text in the highlighted area: What is your favorite color: and add another textbox to the right of the typed text.
[Click to see full-size]
The goal for this article is to show the user the information that was entered in each step once all of the steps have been completed. To accomplish this, a panel needs to be added below the Wizard control and set the visible property to false. Then drag a label inside the panel.
[Click to see full-size]
Now, to be able to capture the entered information as the user proceeds from one step to another, the Wizard control has an event that can be used to achieve this. So in the Source view of the aspx page, add the following to the properties of the Wizard control:
Try Server Intellect for Windows Server Hosting. Quality and Quantity!
Now open up the C# code behind and add the event. As the user proceed from one step to another, the text entered will be saved in session variables that will be used at the end. This is only being done to show the capabilities of the Wizard control, otherwise the textbox text properties could be used directly. First add the Wizard1_ActiveStepChanged event and save the textbox text values for each steps one and two. The other common events that can be used are the CancelButtonClick, FinishButtonClick, NextButtonClick, and PreviousButtonClick events. The only other one used in this article will be the FinishButtonClick event. Go ahead and add that one as well to the Source view in the Wizard1 properties.
OnFinishButtonClick="Wizard1_FinishButtonClick"
Then in the code behind add code that will make the Wizard control go invisible and the panel visible. Set the label text property to display the user’s name, their nationality and their favorite color by retrieving the values from the session variables used above. The final code will look something like this.
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WizardDemo
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Wizard1_ActiveStepChanged(object sender, EventArgs e)
{
// Get the current wizard step
String currentWizardStep = Wizard1.ActiveStep.Name;
if (currentWizardStep == "Step 1")
{
Session["Name"] = TextBox1.Text;
}
else if (currentWizardStep == "Step 2")
{
Session["Nation"] = TextBox2.Text;
}
}
protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{
string name = Session["Name"].ToString();
string nation = Session["Nation"].ToString();
string color = TextBox3.Text;
Wizard1.Visible = false;
Panel1.Visible = true;
Label1.Text = "Hello " + name + " from the " + nation + ". Your favorite color is " + color;
}
}
}
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
Now that everything is in place, run the application to see the results. So Step 1 will look like this:
[Click to see full-size]
Step 2:
[Click to see full-size]
Step 3:
[Click to see full-size]
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
Click the Finish button:
[Click to see full-size]
As shown, the Wizard control is a simple way to have the user enter information in sequential steps without having to write a lot of custom code.
What have we learned?
How to use a Wizard control on a web page.
How to use the ActiveStepChanged event of the Wizard control to capture information
How to use the FinishButtonClick event of the Wizard control to perform some custom code.
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
Attachments
|