Fredrik Normén's Blog - NSQUARED²
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Microsoft Most Valuable Professional
     .Net Framework - ASP.Net - Architecture - Development
NOTE: This list of posts will only list the 15 latest posts, to see the rest, select from the Archives located in the menu to the left. The RSS will only list the 10 lastest posts.
The CreateUserWizard in ASP.Net 2.0

Category:  ASP.Net 2.0

This post will give you an overview about how we can use the CreateUserWizard to add roles to a user and add additional information to the created user.

 

Note: This post is for beginners that haven’t used the CreateUserWizard before.

 

The CreateUserWizard is a wizard control that will display different step when we are creating a user. We can also extend the wizard with our own steps.

 

There are some predefined steps that we can use:

 

WizardStep

 

With the WizardStep we can create our own step by adding our own controls to it:

 

<asp:WizardStep ID="WizardStep1" Runat="server" Title="Custom information" StepType="Step">

   <br/>

   <asp:label runat=server id=infoLabel>Information</asp:label>

   <asp:TextBox runat=server id=informationtexsBox” ..>

   ...

 </asp:WizardStep>

 

TemplatedWizardStep

 

The TemplatedWizard can be used to have more control over how the steps should look like, for example adding our own navigation buttons to the step etc.

 

<asp:TemplatedWizardStep ID="TemplatedWizardStep1" Runat="server">

     <ContentTemplate/>

     <CustomNavigationTemplate/>

</asp:TemplatedWizardStep>

 

With the ContentTemplate we can create our own form for the step. If we want to add our own navigation buttons we use the CustomNavigationTemplate:

 

     <CustomNavigationTemplate>

        <asp:Button Runat="server" Text="Create User"                                  CommandName="MoveNext" ID="StepNextButton" />

     </CustomNavigationTemplate>

 

 

CreateUserWizardStep

 

The CreateUserWizardStep is a step that will show us a form where we can enter user name, password, e-mail address etc. This step will be added automatically when we drag and drop the CreateUserWizard form the toolbox to our page.

 

CompleteWizardStep

 

With the CompletWizardStep step could be used to display a message etc after the Wizard has reach its last step.

 

All the above steps are inhering form the WizardStepBase class.

 

We can create our own step by creating controls that inherits the WizardStep control or one of the other wizard steps classes.

 

There are different kind of template that we can use to customize the wizard, such as the HeaderTemplate, StartNaviagtionTemplate, FinishNavigationTemplate and StepNavigationTemplate etc. I suppose you can figurate out was those template are used for so I will not spend some time writing about them in this post.

 

Before I’m going to show an example of the CreateUserWizard I will explain some of the CreateUserWizard’s events that I’m going to use in the example code of this post:

 

NextButtonClick

 

The NextButtonClick event will be raised when the next button of the current step is pressed.

By using the NextButtonClick’s WizardNavigationEventArgs argument we can get the index of which step we are currently on and we can also get the next index of the next step:

 

if (e.CurrentStepIndex == 1)

    //Do somthing...

 

int nextIndex = e.NextStepIndex;

 

ContinueButtonClick

 

The ContinueButtonClick event will be raised when we press the continue button, this button will be displayed if we user the CompleteWizardStep. In this event we could for example navigate from the wizard to another page, or got to the first step of the CreateUserWizard if we want to add another user.

 

There are a several of other events that I’m not going to use in this tutorial, such as the CancelButtonClick (this event will be raised when the cancel button is pressed), FinishButtonClick (this event will be raised when the finish button is pressed), LoggedIn (this event will be raised when the user is logged in), LoggingIn (this event is raised before the created user is going to be authenticated) and SendingMail (this event will be raised before an e-mail with the user information is sent to the created user’s specified e-mail address) etc.

 

Note: With the CreateUserWizard we could send a e-mail to the created user with our own information etc.

 

Let’s take a look at an example of how we can use the CreateUserWizard. The following example will display a CreateUserWizard with four steps, create user, add roles to user, and add additional information to the user and the complete step:

 

<%@ Page Language="C#" ClassName="Default_aspx" %>

<%@ Import Namespace="System.Collections.Generic" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<script runat=server>

   

    void Page_Load()

    {

        if (!Page.IsPostBack)

        {

            CheckBoxList1.DataSource = Roles.GetAllRoles();

            CheckBoxList1.DataBind();

        }

    }

 

    private void AddInformationToUser()

    {

        Profile.FirstName = firstNameTextBox.Text;

        Profile.LastName = lastNameTextBox.Text;

    }

 

    private void AddSelectedRolesToUser()

    {

        List<string> roles = new List<string>();

 

        foreach (ListItem item in CheckBoxList1.Items)

        {

            if (item.Selected)

                roles.Add(item.Value);

        }

 

        if (roles.Count > 0)

            Roles.AddUserToRoles(CreateUserWizard1.UserName, roles.ToArray());

    }

 

 

    void CreateUserWizard1_ContinueButtonClick(object sender, EventArgs e)

    {

        Response.Redirect("default2.aspx");

    }

 

    void CreateUserWizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)

    {

        if (e.CurrentStepIndex == 1)

            this.AddSelectedRolesToUser();

        if (e.CurrentStepIndex == 2)

            this.AddInformationToUser();

    }

     

</script>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:CreateUserWizard ID="CreateUserWizard1" Runat="server" OnContinueButtonClick="CreateUserWizard1_ContinueButtonClick" OnNextButtonClick="CreateUserWizard1_NextButtonClick">

            <WizardSteps>

                <asp:CreateUserWizardStep Runat="server" Title="Sign Up for Your New Account">

                </asp:CreateUserWizardStep>

                <asp:WizardStep ID="AddRolesStep" Runat="server" Title="Add Roles" StepType="Step">

                    <table>

                        <tr>

                            <td>Select role(s) to be added to the user:</td>

                        </tr>

                        <tr>

                            <td>

                                <asp:CheckBoxList ID="CheckBoxList1" Runat="server">

                                </asp:CheckBoxList>

                            </td>

                        </tr>

                    </table>

                </asp:WizardStep>

                <asp:WizardStep ID="AddInformationStep" Runat="server" Title="Custom information" StepType="Step">

                    <table>

                        <tr>

                            <td colspan="2">Information</td>

                        </tr>

                        <tr>

                            <td>

                                First name:

                            </td>

                            <td>

                                <asp:TextBox ID="firstNameTextBox" Runat="server"></asp:TextBox>

                           </td>

 

                        </tr>

                        <tr>

                            <td>

                                Last name:

                            </td>

                            <td>

                                <asp:TextBox ID="lastNameTextBox" Runat="server"></asp:TextBox>

                           </td>

                        </tr>

                    </table>

                </asp:WizardStep>

                <asp:CompleteWizardStep Runat="server" Title="Complete">

                </asp:CompleteWizardStep>

            </WizardSteps>

        </asp:CreateUserWizard>

       

    </div>

    </form>

</body>

</html>

 

As you can see in the example above I have added two WizardStep controls that will display one form which a CheckBoxList with a list of all the roles that we can add to the created user ("Add roles to user"). I decide to bind the roles to the CheckBoxList in the Page_Load event:

 

void Page_Load()

{

      if (!Page.IsPostBack)

     {

         CheckBoxList1.DataSource = Roles.GetAllRoles();

         CheckBoxList1.DataBind();

      }

}

 

The second wizard step is a form where we can set the first name and last name of the user ("Custom Information").

 

When we run the example above we will first see CreateUserWizardStep (this step will show us a form where we can enter information about the user we are going to create, such as, user name and password etc). The CreateUserWizardStep will also add a Create User button, when this button is pressed the user will be created and logged in to the application. We will also be navigated to next step of the wizard (Add roles to user). In the next step we could select which roles we want to add to the user. After we have selected the roles we want to add, we can press the next button to go to the next step. When the next button is press the NextButtonClick event will be raised:

 

void CreateUserWizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)

{

    if (e.CurrentStepIndex == 1)

         this.AddSelectedRolesToUser();

    if (e.CurrentStepIndex == 2)

         this.AddInformationToUser();

}

 

In the NextButtonClickEvent, there will be a check for which step of the wizard we are navigating from. This is done by using the CurrentStepIndex of the event argument (The CurrentStepIndex is zero based). If we are navigation from the second step (Add roles to user) the AddSelectedRolesToUser will be executed:

 

private void AddSelectedRolesToUser()

{

     List<string> roles = new List<string>();

 

     foreach (ListItem item in CheckBoxList1.Items)

     {

         if (item.Selected)

             roles.Add(item.Value);

     }

     if (roles.Count > 0)

         Roles.AddUserToRoles(CreateUserWizard1.UserName, roles.ToArray());

 }

 

The code above will iterate through the check boxes that contain roles and add the selected roles to a List. If the List contains any roles they will added to the created user (By using the CreateUserWizard1.UserName property, we can get the name of the user we have created).

 

Note: This example will not check if the roles feature are enabled, I assumed that it’s enabled. If not we could add a simple check to check if the roles feature are enabled or not. If it’s not enabled we could jump over the “Add Roles to user Step”:

 

void CreateUserWizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)

{

     if( !Roles.Enabled )

    {

        if (e.CurrentStepIndex == 0)

             CreateUserWizard1.ActiveStepIndex = 2;

     }

     .....

}

 

After the roles have been added, we will be navigated to the next step of the wizard where we can set the first name and last name of the user. When the next button is pressed the CurrentStepIndex will be 2 and in the NextButtonClick event the first name and last name will be added to the user:

 

void CreateUserWizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)

{

  

   if (e.CurrentStepIndex == 2)

      this.AddInformationToUser();

}

 

private void AddInformationToUser()

{

    Profile.FirstName = firstNameTextBox.Text;

    Profile.LastName = lastNameTextBox.Text;

}

 

The AddInformationToUser method will use the Profile feature to set the first and last name of the user.

 

Note: The first name and last name must be specified in the web.config:

 

<profile>

   <properties>

    <add name="FirstName" type="System.String" />

    <add name="LastName" type="System.String" />

   </properties>

</profile>

 

After the first and last name is added to the user, we will be navigated to the complete wizard step. If we press the continue button the ContinueButtonClick event will be raised and we will be navigated to another page:

 

void CreateUserWizard1_ContinueButtonClick(object sender, EventArgs e)

{

    Response.Redirect("default2.aspx");

}

 

When we are done with the wizard, we maybe don’t want to redirect to another page, instead we would like restart the wizard to add another user.  This could be done by setting the ActiveStepIndex of the CreateUserControl to zero in the ContinueButtonClick event. If we diced to use the wizard to only add users, we have to make sure so the created user will not be logged in when it’s been created. By setting the LoginCreatedUser proeprty of the CreateUserWizard to false, the created user will not be logged in. When the user will not be logged in, we can’t use the Profile feature in the way we do it in the AddInformationToUser method. Instead we have to get the profile for the user we are creating. It could be done by using the GetProfile method of the Profile class:

 

private void AddInformationToUser()

{

        ASP.HttpProfile profile = Profile.GetProfile(CreateUserWizard1.UserName);

        profile.FirstName = firstNameTextBox.Text;

        profile.LastName = lastNameTextBox.Text;

}

 

I hope this have give you a little more information about how you can extend the CreateUserWizard. If you have any questions don’t hesitate to ask me.

 

Posted: Wednesday, July 07, 2004 - 16:02 GMT+1    Print     E-mail    Comments (11)
   fredrik.nsquared2.com - 2007