HOW TO:自訂 PasswordRecovery 控制項
更新:2007 年 11 月
您可以下列方式自訂 PasswordRecovery 控制項:
您可以編輯所顯示的樣板 (Template),並變更控制項的使用者介面 (UI)。
您可以將 ASP.NET 主題套用至控制項。如需詳細資訊,請參閱 ASP.NET 佈景主題和面板概觀。
您可以使用控制項的屬性 (例如 QuestionLabelText 或 InstructionTextStyle) 修改其外觀。如需詳細資訊,請參閱類別庫 (Class Library) 參考中 PasswordRecovery 控制項的屬性。
本主題描述如何使用樣板自訂 System.Web.UI.WebControls.PasswordRecovery 控制項。
若要編輯 PasswordRecovery 樣板
將 PasswordRecovery 控制項加入網頁,如下列程式碼範例所示。
<asp:PasswordRecovery ID="PasswordRecovery1" Runat="server"> </asp:PasswordRecovery>
您可以套用樣式,以自訂 PasswordRecovery 控制項的外觀。使用屬性將任何需要的樣式套用至樣板,例如 LabelStyle、HyperLinkStyle 和 TitleTextStyle 屬性。
若要自訂 PasswordRecovery 控制項的內容,請繼續進行下列步驟,以修改 PasswordRecovery 控制項所使用的樣板。
建立 UserNameTemplate 樣板,以指定控制項第一次呈現時出現的標記和控制項,如下列程式碼範例所示。
安全性注意事項: 這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。ASP.NET Web 網頁預設會驗證使用者輸入,但不包含當中的指令碼或 HTML 項目。如需詳細資訊,請參閱指令碼攻擊概觀。
<UserNameTemplate> <table border="0" cellpadding="1"> <tr> <td> <table border="0" cellpadding="0"> <tr> <td align="center" colspan="2"> Forgot Your Password?</td> </tr> <tr> <td align="center" colspan="2"> Enter your User Name to receive your password.</td> </tr> <tr> <td align="right"> <asp:Label ID="UserNameLabel" AssociatedControlID="UserName">User Name:</asp:Label></td> <td> <asp:TextBox ID="UserName" ></asp:TextBox> <asp:RequiredFieldValidator ID="UserNameRequired" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="PasswordRecovery1">*</asp:RequiredFieldValidator> </td> </tr> <tr> <td align="center" colspan="2" style="color: red"> <asp:Literal ID="FailureText" EnableViewState="False"></asp:Literal> </td> </tr> <tr> <td align="right" colspan="2"> <asp:Button ID="SubmitButton" CommandName="Submit" Text="Submit" ValidationGroup="PasswordRecovery1" /> </td> </tr> </table> </td> </tr> </table> </UserNameTemplate>
<UserNameTemplate> <table border="0" cellpadding="1"> <tr> <td> <table border="0" cellpadding="0"> <tr> <td align="center" colspan="2"> Forgot Your Password?</td> </tr> <tr> <td align="center" colspan="2"> Enter your User Name to receive your password.</td> </tr> <tr> <td align="right"> <asp:Label ID="UserNameLabel" AssociatedControlID="UserName">User Name:</asp:Label></td> <td> <asp:TextBox ID="UserName" ></asp:TextBox> <asp:RequiredFieldValidator ID="UserNameRequired" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="PasswordRecovery1">*</asp:RequiredFieldValidator> </td> </tr> <tr> <td align="center" colspan="2" style="color: red"> <asp:Literal ID="FailureText" EnableViewState="False"></asp:Literal> </td> </tr> <tr> <td align="right" colspan="2"> <asp:Button ID="SubmitButton" CommandName="Submit" Text="Submit" ValidationGroup="PasswordRecovery1" /> </td> </tr> </table> </td> </tr> </table> </UserNameTemplate>
建立 QuestionTemplate 樣板,以指定控制項提示使用者安全性問題時出現的標記和控制項,如下列程式碼範例所示。
安全性注意事項: 這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。ASP.NET Web 網頁預設會驗證使用者輸入,但不包含當中的指令碼或 HTML 項目。如需詳細資訊,請參閱指令碼攻擊概觀。
<QuestionTemplate> <table border="0" cellpadding="1"> <tr> <td> <table border="0" cellpadding="0"> <tr> <td align="center" colspan="2"> Identity Confirmation</td> </tr> <tr> <td align="center" colspan="2"> Answer the following question to receive your password.</td> </tr> <tr> <td align="right"> User Name:</td> <td> <asp:Literal ID="UserName" ></asp:Literal> </td> </tr> <tr> <td align="right"> Question:</td> <td> <asp:Literal ID="Question" ></asp:Literal> </td> </tr> <tr> <td align="right"> <asp:Label ID="AnswerLabel" AssociatedControlID="Answer">Answer:</asp:Label></td> <td> <asp:TextBox ID="Answer" ></asp:TextBox> <asp:RequiredFieldValidator ID="AnswerRequired" ControlToValidate="Answer" ErrorMessage="Answer is required." ToolTip="Answer is required." ValidationGroup="PasswordRecovery1">*</asp:RequiredFieldValidator> </td> </tr> <tr> <td align="center" colspan="2" style="color: red"> <asp:Literal ID="FailureText" EnableViewState="False"></asp:Literal> </td> </tr> <tr> <td align="right" colspan="2"> <asp:Button ID="SubmitButton" CommandName="Submit" Text="Submit" ValidationGroup="PasswordRecovery1" /> </td> </tr> </table> </td> </tr> </table> </QuestionTemplate>
<QuestionTemplate> <table border="0" cellpadding="1"> <tr> <td> <table border="0" cellpadding="0"> <tr> <td align="center" colspan="2"> Identity Confirmation</td> </tr> <tr> <td align="center" colspan="2"> Answer the following question to receive your password.</td> </tr> <tr> <td align="right"> User Name:</td> <td> <asp:Literal ID="UserName" ></asp:Literal> </td> </tr> <tr> <td align="right"> Question:</td> <td> <asp:Literal ID="Question" ></asp:Literal> </td> </tr> <tr> <td align="right"> <asp:Label ID="AnswerLabel" AssociatedControlID="Answer">Answer:</asp:Label></td> <td> <asp:TextBox ID="Answer" ></asp:TextBox> <asp:RequiredFieldValidator ID="AnswerRequired" ControlToValidate="Answer" ErrorMessage="Answer is required." ToolTip="Answer is required." ValidationGroup="PasswordRecovery1">*</asp:RequiredFieldValidator> </td> </tr> <tr> <td align="center" colspan="2" style="color: red"> <asp:Literal ID="FailureText" EnableViewState="False"></asp:Literal> </td> </tr> <tr> <td align="right" colspan="2"> <asp:Button ID="SubmitButton" CommandName="Submit" Text="Submit" ValidationGroup="PasswordRecovery1" /> </td> </tr> </table> </td> </tr> </table> </QuestionTemplate>
建立 SuccessTemplate 樣板,以指定使用者成功擷取密碼時出現的標記和控制項
下列程式碼範例示範了已定義 SuccessTemplate 樣板之 PasswordRecovery 控制項的標記。
安全性注意事項: 這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。ASP.NET Web 網頁預設會驗證使用者輸入,但不包含當中的指令碼或 HTML 項目。如需詳細資訊,請參閱指令碼攻擊概觀。
<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script > </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head > <meta http-equiv="Content-Type" content="text/html" /> <title>PasswordRecovery All Templates Sample</title> </head> <body> <form id="form1" > <div title="All Templates Sample"> <asp:PasswordRecovery ID="PasswordRecovery1" > <QuestionTemplate> <table border="0" cellpadding="1"> <tr> <td> <table border="0" cellpadding="0"> <tr> <td align="center" colspan="2"> Identity Confirmation</td> </tr> <tr> <td align="center" colspan="2"> Answer the following question to receive your password.</td> </tr> <tr> <td align="right"> User Name:</td> <td> <asp:Literal ID="UserName" ></asp:Literal> </td> </tr> <tr> <td align="right"> Question:</td> <td> <asp:Literal ID="Question" ></asp:Literal> </td> </tr> <tr> <td align="right"> <asp:Label ID="AnswerLabel" AssociatedControlID="Answer">Answer:</asp:Label></td> <td> <asp:TextBox ID="Answer" ></asp:TextBox> <asp:RequiredFieldValidator ID="AnswerRequired" ControlToValidate="Answer" ErrorMessage="Answer is required." ToolTip="Answer is required." ValidationGroup="PasswordRecovery1">*</asp:RequiredFieldValidator> </td> </tr> <tr> <td align="center" colspan="2" style="color: red"> <asp:Literal ID="FailureText" EnableViewState="False"></asp:Literal> </td> </tr> <tr> <td align="right" colspan="2"> <asp:Button ID="SubmitButton" CommandName="Submit" Text="Submit" ValidationGroup="PasswordRecovery1" /> </td> </tr> </table> </td> </tr> </table> </QuestionTemplate> <UserNameTemplate> <table border="0" cellpadding="1"> <tr> <td> <table border="0" cellpadding="0"> <tr> <td align="center" colspan="2"> Forgot Your Password?</td> </tr> <tr> <td align="center" colspan="2"> Enter your User Name to receive your password.</td> </tr> <tr> <td align="right"> <asp:Label ID="UserNameLabel" AssociatedControlID="UserName">User Name:</asp:Label></td> <td> <asp:TextBox ID="UserName" ></asp:TextBox> <asp:RequiredFieldValidator ID="UserNameRequired" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="PasswordRecovery1">*</asp:RequiredFieldValidator> </td> </tr> <tr> <td align="center" colspan="2" style="color: red"> <asp:Literal ID="FailureText" EnableViewState="False"></asp:Literal> </td> </tr> <tr> <td align="right" colspan="2"> <asp:Button ID="SubmitButton" CommandName="Submit" Text="Submit" ValidationGroup="PasswordRecovery1" /> </td> </tr> </table> </td> </tr> </table> </UserNameTemplate> <SuccessTemplate> <table border="0" cellpadding="1"> <tr> <td> <table border="0" cellpadding="0"> <tr> <td> Your password has been sent to you.</td> </tr> </table> </td> </tr> </table> </SuccessTemplate> </asp:PasswordRecovery> </div> </form> </body> </html>
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script > </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head > <title>Password Recovery All Templates Sample</title> </head> <body> <form id="form1" > <div title="All Templates Sample"> <asp:PasswordRecovery ID="PasswordRecovery1" > <QuestionTemplate> <table border="0" cellpadding="1"> <tr> <td> <table border="0" cellpadding="0"> <tr> <td align="center" colspan="2"> Identity Confirmation</td> </tr> <tr> <td align="center" colspan="2"> Answer the following question to receive your password.</td> </tr> <tr> <td align="right"> User Name:</td> <td> <asp:Literal ID="UserName" ></asp:Literal> </td> </tr> <tr> <td align="right"> Question:</td> <td> <asp:Literal ID="Question" ></asp:Literal> </td> </tr> <tr> <td align="right"> <asp:Label ID="AnswerLabel" AssociatedControlID="Answer">Answer:</asp:Label></td> <td> <asp:TextBox ID="Answer" ></asp:TextBox> <asp:RequiredFieldValidator ID="AnswerRequired" ControlToValidate="Answer" ErrorMessage="Answer is required." ToolTip="Answer is required." ValidationGroup="PasswordRecovery1">*</asp:RequiredFieldValidator> </td> </tr> <tr> <td align="center" colspan="2" style="color: red"> <asp:Literal ID="FailureText" EnableViewState="False"></asp:Literal> </td> </tr> <tr> <td align="right" colspan="2"> <asp:Button ID="SubmitButton" CommandName="Submit" Text="Submit" ValidationGroup="PasswordRecovery1" /> </td> </tr> </table> </td> </tr> </table> </QuestionTemplate> <UserNameTemplate> <table border="0" cellpadding="1"> <tr> <td> <table border="0" cellpadding="0"> <tr> <td align="center" colspan="2"> Forgot Your Password?</td> </tr> <tr> <td align="center" colspan="2"> Enter your User Name to receive your password.</td> </tr> <tr> <td align="right"> <asp:Label ID="UserNameLabel" AssociatedControlID="UserName">User Name:</asp:Label></td> <td> <asp:TextBox ID="UserName" ></asp:TextBox> <asp:RequiredFieldValidator ID="UserNameRequired" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="PasswordRecovery1">*</asp:RequiredFieldValidator> </td> </tr> <tr> <td align="center" colspan="2" style="color: red"> <asp:Literal ID="FailureText" EnableViewState="False"></asp:Literal> </td> </tr> <tr> <td align="right" colspan="2"> <asp:Button ID="SubmitButton" CommandName="Submit" Text="Submit" ValidationGroup="PasswordRecovery1" /> </td> </tr> </table> </td> </tr> </table> </UserNameTemplate> <SuccessTemplate> <table border="0" cellpadding="1"> <tr> <td> <table border="0" cellpadding="0"> <tr> <td> Your password has been sent to you.</td> </tr> </table> </td> </tr> </table> </SuccessTemplate> </asp:PasswordRecovery> </div> </form> </body> </html>