This is the first time I am trying to use the CreateUserWizard control. I seen examples on the web which I have utilized below, which traps an error that occurs when a user tries to register and does something invalid, like try to register using a username that is already registered.
It seems to me that I should not have to use a custom routine like GetErrorMessage to translate an error StatusCode into a custom message; it seems like the following routine somehow be used to determine the error that occurred and get it to be displayed, but I am not sure how or how to find the set the appropriate control with the value of the error text.
Can someone suggest how to handle this?
Protected Sub CreateUserWizard1_CreateUserError(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CreateUserErrorEventArgs) Handles CreateUserWizard1.CreateUserError
End Sub
Markup:
Title="" Language="VB" MasterPageFile="~/Master Pages/MasterPage.master" AutoEventWireup="false" CodeFile="Register.aspx.vb" Inherits="Register" %>
<%@ MasterType VirtualPath="~/Master Pages/MasterPage.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<link href="CSS/Register.css" rel="stylesheet" type="text/css" />
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="phPageContent" Runat="Server">
<h1 style="margin-top:30px;">Register</h1>
<hr />
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server"
CreateUserButtonText="Register"
ContinueDestinationPageUrl="~/Secure/PromotePandaVisa.aspx">
<SideBarTemplate>
Sidebar template
</SideBarTemplate>
<WizardSteps>
<asp:CreateUserWizardStep runat="server" >
<ContentTemplate>
<table border="0" cellpadding="2" cellspacing="2">
<tr>
<td align="center" class="CaptionLabel" colspan="2">
Sign Up for Your New Account</td>
<td align="center" class="CaptionLabel">
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName"
CssClass="DefaultLabelCaption">User Name:</asp:Label>
</td>
<td>
<asp:TextBox ID="UserName" runat="server"
CssClass="DefaultTextBox TextboxWidth"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="CreateUserWizard1">Required</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password"
CssClass="DefaultLabelCaption">Password:</asp:Label>
</td>
<td>
<asp:TextBox ID="Password" runat="server"
CssClass="DefaultTextBox TextboxWidth" TextMode="Password"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="CreateUserWizard1">Required</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="ConfirmPasswordLabel" runat="server"
AssociatedControlID="ConfirmPassword" CssClass="DefaultLabelCaption">Confirm Password:</asp:Label>
</td>
<td>
<asp:TextBox ID="ConfirmPassword" runat="server"
CssClass="DefaultTextBox TextboxWidth" TextMode="Password"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" ControlToValidate="ConfirmPassword" ErrorMessage="Confirm Password is required." ToolTip="Confirm Password is required." ValidationGroup="CreateUserWizard1">Required</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email"
CssClass="DefaultLabelCaption">E-mail:</asp:Label>
</td>
<td>
<asp:TextBox ID="Email" runat="server" CssClass="DefaultTextBox TextboxWidth"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email" ErrorMessage="E-mail is required." ToolTip="E-mail is required." ValidationGroup="CreateUserWizard1">Required</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="QuestionLabel" runat="server" AssociatedControlID="Question"
CssClass="DefaultLabelCaption">Security Question:</asp:Label>
</td>
<td>
<asp:TextBox ID="Question" runat="server"
CssClass="DefaultTextBox TextboxWidth"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="QuestionRequired" runat="server" ControlToValidate="Question" ErrorMessage="Security question is required." ToolTip="Security question is required." ValidationGroup="CreateUserWizard1">Required</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="AnswerLabel" runat="server" AssociatedControlID="Answer"
CssClass="DefaultLabelCaption">Security Answer:</asp:Label>
</td>
<td>
<asp:TextBox ID="Answer" runat="server" CssClass="DefaultTextBox TextboxWidth"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="AnswerRequired" runat="server" ControlToValidate="Answer" ErrorMessage="Security answer is required." ToolTip="Security answer is required." ValidationGroup="CreateUserWizard1">Required</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="center" colspan="2">
</td>
<td align="center">
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword" Display="Dynamic" ErrorMessage="The Password and Confirmation Password must match." ValidationGroup="CreateUserWizard1"></asp:CompareValidator>
</td>
<td align="center">
</td>
</tr>
<tr>
<td align="center" colspan="2" style="color: Red;">
<asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
</td>
<td align="center" style="color: Red;">
</td>
</tr>
</table>
</ContentTemplate>
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep runat="server" >
<ContentTemplate>
<table border="0">
<tr>
<td align="center">
Complete</td>
</tr>
<tr>
<td>
Your account has been successfully created.</td>
</tr>
<tr>
<td align="right">
<asp:Button ID="ContinueButton" runat="server" CausesValidation="False"
CommandName="Continue" Text="Continue" ValidationGroup="CreateUserWizard1" OnPreRender="StepNextButton_PreRender" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:CompleteWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
</asp:Content>
Code Behind:
Partial Class Register
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Master.CurrentSubTab = MasterPage2.SubTabEnum.Affiliate
End Sub
Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateUserWizard1.CreatedUser
Roles.AddUserToRole(CreateUserWizard1.UserName, "Affiliate")
End Sub
Protected Sub CreateUserWizard1_CreateUserError(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CreateUserErrorEventArgs) Handles CreateUserWizard1.CreateUserError
End Sub
Protected Sub CreateUserWizard1_CreatingUser(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs) Handles CreateUserWizard1.CreatingUser
e.Cancel = True
'Membership.RequiresQuestionAndAnswer = False
Try
Dim MyUser As MembershipUser = Membership.CreateUser(CreateUserWizard1.UserName, _
CreateUserWizard1.Password, _
CreateUserWizard1.Email)
Roles.AddUserToRole(CreateUserWizard1.UserName, "Affiliate")
MyUser.Email = CreateUserWizard1.Email
Membership.UpdateUser(MyUser)
Catch ex As MembershipCreateUserException
Dim Message As String = GetErrorMessage(ex.StatusCode)
End Try
End Sub
Public Function GetErrorMessage(ByVal status As MembershipCreateStatus) As String
Select Case status
Case MembershipCreateStatus.DuplicateUserName
Return "Username already exists. Please enter a different user name."
Case MembershipCreateStatus.DuplicateEmail
Return "A username for that e-mail address already exists. Please enter a different e-mail address."
Case MembershipCreateStatus.InvalidPassword
Return "The password provided is invalid. Please enter a valid password value."
Case MembershipCreateStatus.InvalidEmail
Return "The e-mail address provided is invalid. Please check the value and try again."
Case MembershipCreateStatus.InvalidAnswer
Return "The password retrieval answer provided is invalid. Please check the value and try again."
Case MembershipCreateStatus.InvalidQuestion
Return "The password retrieval question provided is invalid. Please check the value and try again."
Case MembershipCreateStatus.InvalidUserName
Return "The user name provided is invalid. Please check the value and try again."
Case MembershipCreateStatus.ProviderError
Return "The authentication provider Returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator."
Case MembershipCreateStatus.UserRejected
Return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator."
Case Else
Return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator."
End Select
End Function
Protected Sub StepNextButton_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
Me.Page.Form.DefaultButton = CType(sender, Button).UniqueID
End Sub
Protected Sub CreateUserWizard1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateUserWizard1.PreRender
CreateUserWizard1.Focus()
End Sub
End Class