简体   繁体   English

Asp.net上的C#Textchanged事件触发两次

[英]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. 我有一个简单的Webform来更改当前用户的密码,所以我有三个TextBoxes ,一个用于实际密码,另外两个用于确认我要保存的新密码。 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 . 当我尝试写入实际密码时,和我在数据库中输入的密码相同时,应该仅启用用于写入新密码的TextBox ,并且确实可以,但是我在TextChanged事件上放置了一个调试点,它触发了该时间为了启用我的TextBox ,但几乎立即触发,第二次启用了我的第二个TextBox ,但是我丢失了最初在第一个TextBox拥有的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;" 所以这是我的textchanged事件,我在“ 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. 这是一个使用MasterPage的Webform,请注意,这是我遇到此问题的唯一形式。

This is my Aspx Code: 这是我的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>

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. 我发现我的代码出了什么问题,并且是当Textmode设置为“ password”时,ASP会自动删除该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. 我在结束TextChanged事件之前添加了这两行代码,因此,即使执行回发,也不会删除我的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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