共用方式為


ChangePassword.OnChangingPassword(LoginCancelEventArgs) 方法

定義

在成員資格提供者變更使用者的密碼之前,引發 ChangingPassword 事件。

protected:
 virtual void OnChangingPassword(System::Web::UI::WebControls::LoginCancelEventArgs ^ e);
protected virtual void OnChangingPassword (System.Web.UI.WebControls.LoginCancelEventArgs e);
abstract member OnChangingPassword : System.Web.UI.WebControls.LoginCancelEventArgs -> unit
override this.OnChangingPassword : System.Web.UI.WebControls.LoginCancelEventArgs -> unit
Protected Overridable Sub OnChangingPassword (e As LoginCancelEventArgs)

參數

e
LoginCancelEventArgs

包含事件資料的 CancelEventArgs 物件。

範例

下列程式代碼範例示範如何使用使用 ChangePassword 控件的 ASP.NET 頁面,並包含名為 ChangingPasswordChangingPassword事件的處理程式。 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>

備註

呼叫 屬性中指定的MembershipProvider成員資格提供者之前,會OnChangingPassword先呼叫 方法,以變更用戶的密碼。

OnChangingPassword使用 方法來執行變更密碼之前所需的任何處理,例如檢查新密碼以確定它不在一般密碼清單中。

方法OnChangingPassword可以將傳遞為 e 參數的物件CancelEventArgs屬性設定Canceltrue,以取消ChangingPassword事件。

引發事件會透過委派叫用此事件處理常式。 如需詳細資訊,請參閱 ASP.NET Web Forms Pages 中的伺服器事件處理

OnChangingPassword 方法也允許衍生類別處理事件,而不用附加委派。 這是在衍生類別中處理事件的慣用技巧。

給繼承者的注意事項

OnChangingPassword(LoginCancelEventArgs) 衍生類別中覆寫 方法時,請務必呼叫 OnChangingPassword(LoginCancelEventArgs) 基類的方法,讓已註冊的委派接收 事件。

適用於

另請參閱