2016-12-02 60 views
0

私は現在のユーザーのパスワードを変更する簡単なWebフォームを持っていますので、実際のパスワードはTextBoxes、もう1つは新しいパスワードを確認するだけですセーブ。私の実際のパスワードを書いてみると、私のデータベースにあるパスワードと同じですが、新しいパスワードを書き込むためのTextBoxは有効になっているはずですが、私はTextChangedイベントにデバッグポイントを入れて、 TextBoxを有効にするには、ただちに2回目の起動を行います。私は2回目を持っています。TextBoxを有効にしていましたが、最初は私が最初に持っていたテキストが失われますTextBoxc#Asp.netのTextchangedイベントが2回発砲しています

なぜこれができますか?

protected void txt_passvieja_TextChanged(object sender, EventArgs e) 
    { 
     usuario_BLL bll = new usuario_BLL(); 
     usuario obj = new usuario(); 
     obj = bll.Leer(Session["usuario"].ToString()); 
     if (txt_passvieja.Text == obj.passwordUsuario) 
     { 
      txt_passwordnueva.Enabled = true; 
     } 
     else 
     { 
      lbl_header.Text = "Error"; 
      lbl_body.Text = "La contraseña ingresada no coincide con la base de datos"; 
      Page.ClientScript.RegisterStartupScript(this.GetType(), "myFunction", "myFunction()", true); 
      Limpiar(); 
     } 
    } 

は、これが私のtextchangedイベント、私は私の中にデバッグポイントを追加したさ「txt_passwordnueva.Enabled =真;」テキストを変更すると、このイベントを2回実行することがわかります。 これはMasterPageを使用しているWebフォームですが、これが私がこの問題を抱えている唯一のフォームであることに注意してください。

これは私のASPXコード:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="CambiarContraseña.aspx.cs" Inherits="LegalCaseWeb.CambiarContraseña" %> 
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> 
    <link href="css/login.css" rel="stylesheet" type="text/css" /> 
    <script type="text/javascript"> 
     function myFunction() { 
      $("#contraseña_incorrecta").modal('show'); 
     } 
    </script> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
    <div style="text-align:center; background-color: #ffffff; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);"> 
     <asp:Label ID="lbl_passvieja" runat="server" Text="Contraseña actual: " CssClass="labels"></asp:Label> 
     <asp:TextBox ID="txt_passvieja" runat="server" TextMode="Password" CssClass="textbox" AutoPostBack="true" BackColor="#efefef" OnTextChanged="txt_passvieja_TextChanged"></asp:TextBox> 
     <br /> 
     <br /> 
     <asp:Label ID="lbl_passwordnueva" runat="server" Text="Nueva contraseña: " CssClass="labels"></asp:Label> 
     <asp:TextBox ID="txt_passwordnueva" TextMode="Password" runat="server" AutoPostBack="true" BackColor="#efefef" OnTextChanged="txt_passwordnueva_TextChanged"></asp:TextBox> 
     <br /> 
     <br /> 
     <asp:Label ID="lbl_confirmar" runat="server" Text="Confirme nueva contraseña: " CssClass="labels"></asp:Label> 
     <asp:TextBox ID="txt_confirmar" TextMode="Password" runat="server" AutoPostBack="true" BackColor="#efefef" OnTextChanged="txt_confirmar_TextChanged"></asp:TextBox> 
     <br /> 
     <br /> 
     <asp:Button ID="btn_cambiar" runat="server" Text="Cambiar contraseña" AutoPostBack="true" OnClick="btn_cambiar_Click" /> 
    </div> 
    <div id="contraseña_incorrecta" class="modal fade" role="dialog"> 
     <div class="modal-dialog"> 
      <!-- Modal content--> 
      <div class="modal-content"> 
       <div class="modal-header"> 
        <button type="button" class="close" data-dismiss="modal">&times;</button> 
        <h4 class="modal-title"><asp:Label ID="lbl_header" runat="server"></asp:Label></h4> 
       </div> 
       <div class="modal-body"> 
        <p><asp:Label ID="lbl_body" runat="server"></asp:Label></p> 
       </div> 
       <div class="modal-footer"> 
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       </div> 
      </div> 
     </div> 
    </div> 
</asp:Content> 
+0

イベントを2回発生させるコードを共有できます。 'TextChanged'だけでなく、テキストボックスを使用しているところでも –

+2

あなたの問題を示すために[MCVE]を囲みます。イベントハンドラが2回配線され、イベントの2回の起動が発生することがあります。 – t0mm13b

+0

完了、申し訳ありません、私はここで初心者です –

答えて

0

私は私のコードで間違っていたものを発見し、それがテキストモードは、「パスワード」に設定されている場合、ASPは自動的にあなたがその中に持っている任意のテキストを消去ということですテキストボックス。私がやった溶液を作製するには、このでした:

string pass = txt_password.Text; 
txt_password.Attributes.Add("value", pass); 

それがポストバックを実行した場合でも、それは私のテキストを消去しない方法で、だから私は、私のTextChangedイベントを終了する前に、コードのこれらの2行を追加しました。

関連する問題