Customize CreateUserWizard Control with dropDownList for PasswordQuestion
Basic Steps:
-
Add CreateUserWizard Control to the aspx page (e.g.CreateUser.aspx)
-
Convert it to Template by customizing the CreateUserStep
-
Remove the Question TextBox control and add a dropdownList.
-
Make appropriate name and validation changes
-
Add CreatingUser event handler to CreatUserWizard Control
Here is the code.
Part of CreateUserWizard markup showing the Password Question Dropdownlist:
<tr>
<td align="right"> <asp:Label ID="QuestionLabel" runat="server" AssociatedControlID="QuestionDDL">Security Question:</asp:Label></td> <td><asp:DropDownList ID="QuestionDDL" runat="server" Width="155px"></asp:DropDownList>
<asp:RequiredFieldValidator ID="QuestionRequired" runat="server" ControlToValidate="QuestionDDL"
ErrorMessage="Security question is required." ToolTip="Security question is required." ValidationGroup="CreateUserWizard1" InitialValue="Select a Question">*</asp:RequiredFieldValidator> <asp:TextBox ID="Question" runat="server" Enabled="False" Visible="False"></asp:TextBox> </td> </tr>
The PageLoad event handler for CreateUser.aspx:
protected void Page_Load(object sender, EventArgs e){
if (!IsPostBack){
//Create a questions arraylist for this sample //In real-world you would like to get this from some data storeArrayList pwdQuestion = new ArrayList();pwdQuestion.Add("Select a Question");
pwdQuestion.Add("Q1");pwdQuestion.Add("Q2");
pwdQuestion.Add("Q3"); //Get reference to Questions DropDownList inside CUW and bind it to the datasource DropDownList Questionddl = (DropDownList)CreateUserWizardStep1.ContentTemplateContainer.FindControl("QuestionDDL");Questionddl.DataSource = pwdQuestion;
Questionddl.DataBind();
}
}
Code for CUW's CreatingUser event handler:
protected void CreateUserWizard1_CreatingUser(object sender, LoginCancelEventArgs e){
DropDownList Questionddl = (DropDownList)CreateUserWizardStep1.ContentTemplateContainer.FindControl("QuestionDDL");CreateUserWizard1.Question = Questionddl.SelectedValue;
}
The code is almost self-Explanatory.
Points to Note:
- Note we have not removed the Question TextBox but set its Visible property to False. The reason is if we set requiresQuestionAndAnswer="true" in the web.config for the membership provider and remove the TextBox, we will get an exception that it cannot find TextBox control for Password Question.
- You might want to tweak a bit in your code for Validation to work when you are getting values from Database for the dropdownlist.