How to customize ASP.NET membership PasswordRecovery control in C#

You can customize the PasswordRecovery control completely, by using templates for every view:

–  The UserNameTemplate contains all the controls displayed for the first step of the password recovery process when the user is required to enter the user name.

–  The control of the password question step which is placed in the Question template.

–  As a last step a Success Template that consists of the controls displayed for the confirmation, which are shown after the password has been sent successfully to the user.

Every template has certain required controls. For example:

– the UserNameTemplate requires a text box for entering the user name

– the QuestionTemplate requires a text box for entering the question

– the SuccessTemplate requires a Literal control for displaying the final confirmation message

A template PasswordRecovery control might look like this:

 

<asp:PasswordRecovery ID=”PasswordTemplateCtrl” runat=”server”>

<MailDefinition From=”myemail@mydomain.com”

Priority=”high”

Subject=”Important information” />

<UserNameTemplate>

<div align=”center”>

<font face=”Arial Narrow”>

<h2>Forgotten your Password?</h2>

Please enter your user name:<br />

<asp:TextBox ID=”UserName” runat=”server” /> <br />

<asp:Button ID=”SubmitButton” CommandName=”Submit” runat=”server” Text=”Next” /> <br />

<span style=”color: Red”>

<asp:Literal ID=”FailureText” runat=”server” />

</span>

</font>

</div>

</UserNameTemplate>

<QuestionTemplate>

<div align=”center”>

<font face=”Arial Narrow”>

<h2>Forgotten your Password?</h2>

Hello <asp:Literal ID=”UserName” runat=”server” />! <br />

Please answer your password-question:<br />

<asp:Literal ID=”Question” runat=”server” /><br />

<asp:TextBox ID=”Answer” runat=”server” /><br />

<asp:Button ID=”SubmitButton” CommandName=”Submit” runat=”Server” Text=”Send Answer” /><br />

<asp:Literal ID=”FailureText” runat=”server” />

</font>

</div>

</QuestionTemplate>

<SuccessTemplate>

Your password has been sent to your email address

<asp:Label ID=”EmailLabel” runat=”server” />!

</SuccessTemplate>

</asp:PasswordRecovery>

 

If you use controls with the appropriate ID values and use the appropriate CommandName values for buttons, you don’t have to write any code for the control to work. The next table lists the controls for PasswordRecovery templates:

 

 

Additional Template

ID

Control Type

Required?

Comments

UserNameTemplateUserNameSystem.Web.UI.Web Controls.YesTextBox
UserNameTemplateSubmitButtonAll controls that support Command event bubbling.NoName must be set to Submit.
UserNameTemplateFailureTextSystem.Web.UI.Web Controls.NoLiteral
QuestionTemplateUserNameSystem.Web.UI.Web Controls.NoLiteral
QuestionTemplateQuestionSystem.Web.UI.Web Controls.NoLiteral
QuestionTemplateAnswerSystem.Web.UI.Web Controls.YesTextBox
QuestionTemplateSubmitButtonAll controls that support Command event bubbling.NoName must be set to Submit.
QuestionTemplateFailureTextSystem.Web.UI.Web Controls.NoLiteral

 

 

You can use the controls Button, ImageButton, or LinkButton as submit button, because they support event bubbling and a CommandName property. You have to set CommandName to Submit, otherwise, the command is not recognized by the control. The SuccessTemplate doesn’t require any type of control with any special IDs and you can add any control you want there.It’s just for displaying the confirmation. You can use the FindControl method for finding the control (which is actually a child control of the PasswordRecovery template control) in the appropriate template, as follows:

 

protected void PasswordTemplateCtrl_SendingMail(object sender, MailMessageEventArgs e)

{

Label lbl =(Label)PasswordTemplateCtrl.SuccessTemplateContainer.FindControl(“EmailLabel”);

lbl.Text = e.Message.To[0].Address;

}

 

You cannot call the FindControl method directly on the PasswordRecovery control instance, because the PasswordRecovery control includes more than one template. You have to select the appropriate template container (UserNameTemplateContainer, QuestionTemplateContainer, or SuccessTemplateContainer). Afterward, you can work with the control as usual.