自定义验证无法正常工作

时间:2016-09-19 10:29:57

标签: c# asp.net

如果输入的登录电子邮件地址(用户名)已存在于数据库中,我已编写自定义验证以防止用户将信息保存在数据库中。

 <div class="span3">
                            <asp:TextBox ID="txtLoginEmailAddress" class="SearchTxtBox" runat="server" Text='<%# ds.Tables[0].Rows[0]["LoginEmailAddress"] %>'
                                MaxLength="50"> </asp:TextBox>
                         </div>
                         <div class="span6">
                            <asp:RequiredFieldValidator ID="valLoginEmailAddressRequired" runat="server" CssClass="Validator"
                                ValidationGroup="save" Display="Dynamic" ControlToValidate="txtLoginEmailAddress"
                                ErrorMessage="<b>User Name is required</b>"></asp:RequiredFieldValidator>
                             <asp:CustomValidator ID="CheckAvailablity" runat="server" ValidationGroup="save" ControlToValidate="txtLoginEmailAddress"
                              OnServerValidate="ValidateUserName"></asp:CustomValidator>

             <asp:Label ID="valDuplicatePassword" runat="server" style="color:red" Visible="False"></asp:Label></td>
                        </div>

       <asp:Button ID="btnSave" runat="server" Text="Save" CssClass="btn btn-small " OnClick="btnSave_Click"
                                ValidationGroup="save" />

在cs文件中

       protected void ValidateUserName(object sender, ServerValidateEventArgs e)
    {
        SystemUserBL bl = new SystemUserBL(SessionContext.SystemUser);
        ds = new DataSet();
        bl.FetchForLoginEmailAddress(ds, txtLoginEmailAddress.Text);

        if (ds.Tables[0].Rows.Count > 0)
        {
            e.IsValid = false;
            valDuplicatePassword.Visible = true;
            valDuplicatePassword.Text = "<b>This User Name is already in use by another user.</b>";
            btnSave.Enabled = false;
            btnSaveclose.Enabled = false;
        }
        else
        {
            e.IsValid = true;
            btnSave.Enabled = true;
            btnSaveclose.Enabled = true;
            valDuplicatePassword.Visible = true;
            valDuplicatePassword.Text = "<b>Congratulations! " + txtLoginEmailAddress.Text + " is available.</b>";
        }
    }

       protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (save())
            { 
              SucessMessage();
            }
        }
        catch (Exception ee)
        {
            ErrorMessage(ee.Message);
        }
    }

现在,当我们点击“保存”按钮时,它会在将数据保存到数据库后显示验证消息。我希望它不应该保存在数据库中,自定义验证应该像其他ASP.NET的验证一样工作。请帮忙!!!

1 个答案:

答案 0 :(得分:1)

在继续处理之前,您必须明确检查页面是否有效。所以在你的情况下:

protected void btnSave_Click(object sender, EventArgs e)
{
    try
    {
      if (this.IsValid()) //this checks that the page passed validation, including custom validation
      {
        if (save())
        { 
          SucessMessage();
        }
      }
      else
      {
        //any custom error message (additional to the validators themselves) should go here
      }
    }
    catch (Exception ee)
    {
        ErrorMessage(ee.Message);
    }
}

严格来说,这适用于所有ASP.NET验证程序,但尤其是在您未使用客户端验证的情况下,您更有可能遇到此问题。即使您的验证器确实启用了客户端,您也应该进行此检查 - 恶意用户很容易绕过客户端验证并提交无效数据。

有关此主题的更详细讨论,请参阅此博客:https://www.jardinesoftware.net/2012/03/31/net-validators-dont-forget-page-isvalid/