简体   繁体   中英

c# Textchanged event on Asp.net is firing twice

I have a simple Webform to change the password of the current user, so I have three TextBoxes , one for the actual password, and two to just confirm the new password I want to save. When I try writing my actual password and it's the same I have on my database, the TextBox to write the new password is supposed to be enabled only, and it does, but I put a debug point on my TextChanged event, it fires that time to enable my TextBox , but almost immediately, it fires a second time, I have my second TextBox enabled, but I lose the text I originally had in my first TextBox .

Any ideas why could this be?

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();
        }
    }

So this is my textchanged event, I have added a debug point in my "txt_passwordnueva.Enabled=true;" and when I change the text, I see that it executes this event twice. This is a Webform using a MasterPage, note that this is the only form where I have this problem.

This is my Aspx Code:

    <%@ 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>

I found out what was wrong with my code, and it is that when the Textmode is set to "password", ASP automatically erases any text you have in that Textbox. To make a solution what I did was this:

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

I added those two lines of code before ending my TextChanged Event, so in that way even when it executes the postback, it doesn't erases my text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM