ChangePassword.DisplayUserName 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定值,指出 ChangePassword 控制項是否應顯示 UserName 控制項和標籤。
public:
virtual property bool DisplayUserName { bool get(); void set(bool value); };
public virtual bool DisplayUserName { get; set; }
member this.DisplayUserName : bool with get, set
Public Overridable Property DisplayUserName As Boolean
屬性值
如果 ChangePassword 控制項應顯示 UserName,則為 true
,否則為 false
。 預設為 false
。
範例
下列程式代碼範例示範如何設定 DisplayUserName 屬性,將控件顯示 UserName 給未登入網站的使用者。
<%@ page language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(object sender, EventArgs e )
{
if (Context.User.Identity.IsAuthenticated)
{
Changepassword1.DisplayUserName = false;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ChangePassword.DisplayUserName sample.</title>
</head>
<body>
<form id="form1" runat="server">
<div>
User's login status: <asp:loginstatus id="status" runat="server" /><br />
<asp:changepassword id="Changepassword1" runat="server" displayusername="true" />
</div>
</form>
</body>
</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 runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Context.User.Identity.IsAuthenticated Then
changepassword1.DisplayUserName = False
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ChangePassword.DisplayUserName sample.</title>
</head>
<body>
<form id="form1" runat="server">
<div>
User's login status: <asp:loginstatus id="status" runat="server" /><br />
<asp:changepassword id="Changepassword1" runat="server" displayusername="true" />
</div>
</form>
</body>
</html>
下列程式代碼範例示範如何使用使用 ChangePassword 控件的 ASP.NET 頁面,並包含名為 ChangingPassword
之ChangingPassword事件的處理程式。 處理程式中的 ChangingPassword
程式代碼會將儲存在 屬性中的 CurrentPassword 舊密碼與儲存在 中的 NewPassword新密碼進行比較。 如果兩個密碼相同,變更密碼會失敗。
控制項 ChangePassword 會將 DisplayUserName 屬性設定為 true
,讓用戶能夠輸入其用戶名稱。 這表示使用者不需要登入即可檢視頁面。
程式代碼範例假設 ASP.NET 網站已設定為使用 ASP.NET 成員資格和窗體驗證,而且已建立使用者的名稱和密碼已知。 如需詳細資訊,請參閱 如何:實作簡單窗體驗證。
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
//Manually register the event-handling methods.
ChangePassword1.ChangingPassword += new LoginCancelEventHandler(this._ChangingPassword);
}
void _ChangingPassword(Object sender, LoginCancelEventArgs e)
{
if (ChangePassword1.CurrentPassword.ToString() == ChangePassword1.NewPassword.ToString())
{
Message1.Visible = true;
Message1.Text = "Old password and new password must be different. Please try again.";
e.Cancel = true;
}
else
{
//This line prevents the error showing up after a first failed attempt.
Message1.Visible = false;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ChangePassword including a ChangingPassword event handler</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center">
<h1>ChangePassword</h1>
<asp:LoginView ID="LoginView1" Runat="server"
Visible="true">
<LoggedInTemplate>
<asp:LoginName ID="LoginName1" Runat="server" FormatString="You are logged in as {0}." />
<br />
</LoggedInTemplate>
<AnonymousTemplate>
You are not logged in
</AnonymousTemplate>
</asp:LoginView><br />
<asp:ChangePassword ID="ChangePassword1" Runat="server"
BorderStyle="Solid"
BorderWidth="1"
CancelDestinationPageUrl="~/Default.aspx"
DisplayUserName="true"
OnChangingPassword="_ChangingPassword"
ContinueDestinationPageUrl="~/Default.aspx" >
</asp:ChangePassword><br />
<asp:Label ID="Message1" Runat="server" ForeColor="Red" /><br />
<asp:HyperLink ID="HyperLink1" Runat="server"
NavigateUrl="~/Default.aspx">
Home
</asp:HyperLink>
</div>
</form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Public Sub PageLoad(ByVal Sender As Object, ByVal e As EventArgs)
'Manually register the event-handling methods.
AddHandler ChangePassword1.ChangingPassword, AddressOf Me._ChangingPassword
End Sub
Public Sub _ChangingPassword(ByVal Sender As Object, ByVal e As LoginCancelEventArgs)
If (ChangePassword1.CurrentPassword.ToString() = ChangePassword1.NewPassword.ToString) Then
Message1.Visible = True
Message1.Text = "Old password and new password must be different. Please try again."
e.Cancel = True
Else
'This line prevents the error showing up after a first failed attempt.
Message1.Visible = False
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ChangePassword including a ChangingPassword event handler</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center">
<h1>ChangePassword</h1>
<asp:LoginView ID="LoginView1" Runat="server"
Visible="true">
<LoggedInTemplate>
<asp:LoginName ID="LoginName1" Runat="server" FormatString="You are logged in as {0}." />
<br />
</LoggedInTemplate>
<AnonymousTemplate>
You are not logged in
</AnonymousTemplate>
</asp:LoginView><br />
<asp:ChangePassword ID="ChangePassword1" Runat="server"
BorderStyle="Solid"
BorderWidth="1"
CancelDestinationPageUrl="~/Default.aspx"
DisplayUserName="true"
OnChangingPassword="_ChangingPassword"
ContinueDestinationPageUrl="~/Default.aspx" >
</asp:ChangePassword><br />
<asp:Label ID="Message1" Runat="server" ForeColor="Red" /><br />
<asp:HyperLink ID="HyperLink1" Runat="server"
NavigateUrl="~/Default.aspx">
Home
</asp:HyperLink>
</div>
</form>
</body>
</html>
備註
若要變更其密碼,用戶必須由成員資格提供者進行驗證。 若要允許未登入的使用者變更其密碼,或由成員資格提供者使用不同的使用者帳戶進行驗證,然後變更該帳戶的密碼, ChangePassword 控件可以顯示 TextBox 控件以接受用戶名稱。
如果ChangePassword控件要顯示給未登入的使用者,您必須將 DisplayUserName 屬性true
設定為 ,否則使用者將無法指定用戶名稱。