ASP.net更改密码验证器

时间:2014-01-14 19:47:05

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

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

我已完成编程,现在手动测试每个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个新密码框并输入新密码。即使它们为空,即使它们不匹配,密码仍会随第一部分的值而变化。如果它们不匹配或者为null,我不想运行服务器端代码。我究竟做错了什么?帮助非常复杂。

2 个答案:

答案 0 :(得分:2)

将'ValidationGroup ='注册''属性添加到btnChange按钮。

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

Page.Validate("signup");

if (!Page.IsValid)
{
    return;
}

答案 1 :(得分:1)

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