December 2017 : Online courses in ASP.NET MVC and Angular 4. Conducted by Bipin Joshi. Read more...
Registration for December 2017 batches of ASP.NET MVC / Core and Angular 4 online courses have already started. Conducted by Bipin Joshi. Book your seat today ! Click here for more details.

Untitled 1

Customizing CreateUserWizard control to show only Sign Up step

Recently a reader asked:

  • Can CreateUserWizard control be customized to show a predefined Security Questions instead of allowing user to enter his own question?
  • Can CreateUserWizard control be configured such that it shows only one step (Sign Up)? Can the completion step be skipped altogether?

This short post is an attempt to answer these questions. Let's see how above tasks can be accomplished. Have a look at the following CreateUserWizard control:

Observe two things - firstly the Security Questions are displayed in a DropDownList instead of a TextBox (default). Secondly, a success message is displayed at the bottom of the Sign Up step itself instead of displaying the completion step.

The first task is quite easy. Once you drag and drop the CreateUserWizard control on the web form, select its smart tag and select "Customize Create User Step" option. 

This will reveal the template of the Sign Up form. Now, select Edit Templates from the smart tag and choose ContentTemplate.

Then delete the TextBox that is placed in front of Security Question label and place a DropDownList control there. Add whatever predefined questions you would like to display to the end user. Set the ID property of this DropDownList control to Question. This is important because otherwise the CreateUserWizard control won't pick the value selected in the dropdown correctly.

Now, add a Lable control at the bottom of the template and set its ID as lblSuccess. You will display the success message in this label.

Let's code the second task now. Accomplishing the second task requires bit more efforts because you can't remove the Complete wizard step entirely. However, with some trick as outlined here you can accomplish the task. What essentially you need to do is handle two events of the CreateUserWizard control - CreatedUser and ActiveStepChanged.

The CreatedUser event is raised when a user is successfully created by the wizard and the ActiveStepChanged event is raised when the wizard step changes from Sign Up to Complete (or any step for that matter). The following code shows the event handlers for both of these events:

bool flag = false;

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
  flag = true;
}

protected void CreateUserWizard1_ActiveStepChanged(object sender, EventArgs e)
{
  if (flag == true)
  {
    CreateUserWizard1.ActiveStepIndex = 0;
    Label lbl=(Label) CreateUserWizard1.ActiveStep.Controls[0].FindControl("lblSuccess");
    lbl.Text = "User created successfully!";
    flag = false;
  }
}

The above code declares a boolean variable named flag. This variable indicates whether a user is successfully created or not. The CreatedUser event handler sets the flag variable to true indicating that a user is successfully created. The ActiveStepChanged event will be raised as soon as you click on the Create User button and the user creation is successful. Inside this event handler you check the flag variable to ensure that user creation is successful. If so you set the ActiveStepIndex property to 0. This will cause the CreateUserWizard to switch its active step from Complete to Sign Up again. Then you get hold of the Label control that you placed inside the ContentTemplate of Sign Up step. This is done using FindControl() method and specifying the control ID to look for as lblSuccess. Notice that FindControl() is called on Controls[0] and not directly on ActiveStep. Then the Text property of the Label is set to some success message and the flag variable is set to false.

That's it! Run the web form and test if you get the desired functionality in the CreateUserWizard control.

 

 

 


Bipin Joshi is a software consultant, an author and a yoga mentor having 22+ years of experience in software development. He also conducts online courses in ASP.NET MVC / Core and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Meditation and Mindfulness to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 31 July 2013


Tags : ASP.NET Web Forms Server Controls