【问题标题】:ASP.net Change Password ValidatorASP.net 更改密码验证器
【发布时间】:2014-02-03 01:06:57
【问题描述】:

我正在开发一个具有管理面板和购物面板的电子商务项目。

我已经完成了编程,现在手动测试每个 aspx 和 cs 文件。

问题是,我有一个与会话和数据库相关的更改密码功能。问题是我的 aspx 文件中有一个验证器,但它们不起作用。这是我的代码;

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:Panel ID="Panel1" runat="server" DefaultButton="btnChange">
        <div class="userForm">
            <div class="formTitle">
                Change Your Password
            </div>
            <div class="formContent">
                <asp:TextBox ID="txtPassword" placeholder="Type your new password" runat="server" TextMode="Password"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtPassword"
                    ErrorMessage="RequiredFieldValidator" ForeColor="Red" Display="Dynamic" ValidationGroup="signup">Enter a password</asp:RequiredFieldValidator>
                <br />
                <asp:TextBox ID="txtAgainPassword" placeholder="Repeat your new password" runat="server" TextMode="Password"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" BorderColor="Red"
                    ControlToValidate="txtAgainPassword" Display="Dynamic" ErrorMessage="Enter password again."
                    ForeColor="Red" ValidationGroup="signup"></asp:RequiredFieldValidator>
                <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtPassword"
                    ControlToValidate="txtAgainPassword" Display="Dynamic" ErrorMessage="Password don't match."
                    ForeColor="Red" ValidationGroup="signup"></asp:CompareValidator>
                <br />
                <asp:Button ID="btnChange" runat="server" Text="Submit" OnClick="btnChange_Click" />
                <br />
                <asp:Label ID="lblError" Visible="False" ForeColor="Green" runat="server"></asp:Label></div>
        </div>
    </asp:Panel>
</asp:Content>

.cs 部分在下面

protected void btnChange_Click(object sender, EventArgs e)
{
    using (ZirveMarketDBEntities context = new ZirveMarketDBEntities())
    {
        // Atanan sessiona gore user bilgisini al - guvenlik icin onemli
        int id = (int)Session["CustomerID"];
        Customer cust = context.Customers.Where(i => i.CustomerID == id).FirstOrDefault();

        cust.Password = txtPassword.Text;
        context.SaveChanges();
    }
    lblError.Visible = true;
    lblError.Text = "Password successfully updated";
}

问题是,我有一个 2 框输入新密码并输入新密码。即使它们为空,即使它们不匹配密码仍然会随着第一部分的值而变化。如果它们不匹配或为空,我不想运行服务器端代码。我究竟做错了什么?帮助非常感谢。

【问题讨论】:

    标签: c# asp.net sql-server asp.net-mvc-3


    【解决方案1】:

    将 'ValidationGroup="signup"' 属性添加到 btnChange 按钮。

    如果客户端禁用了 Javascript,我还建议将以下内容添加到点击事件中(在其他任何内容之前):

    Page.Validate("signup");
    
    if (!Page.IsValid)
    {
        return;
    }
    

    【讨论】:

    • 啊,我怎么会错过!似乎是时候让我休息一下了:)感谢您的回答,我也会考虑您对我使用的每个按钮的推荐,谢谢!
    • 谢谢。由于某种原因,在使用带有 asp TextBox 控件的占位符时,所需的验证器在 IE9 和 8 中不起作用。将此代码添加到 btn 提交事件修复了它。
    【解决方案2】:

    您在验证器上指定了验证组,但在按钮上没有。尝试将验证组也添加到按钮。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-07
      • 2014-07-27
      • 2023-03-31
      • 2011-09-24
      相关资源
      最近更新 更多