ASP.NET服务器端验证未触发

时间:2014-08-11 09:04:36

标签: c# asp.net .net validation

在我的应用程序服务器端验证功能不起作用。未调用函数。我把调试器放在thuat函数上,但它没有停止调试器.i.e。函数不叫

<asp:TextBox type="text" ID="txtMobilePhone" runat="server" ClientIDMode="Static" CausesValidation="true"/>
                        <asp:CustomValidator ID="cvMobilePhone" runat="server" OnServerValidate="cvMobilePhone_ServerValidate" 
                            Text="Mobile Phone already exist in this Reward Program." ErrorMessage="Mobile Phone already exist in this Reward Program."
                            Display="Dynamic" ValidationGroup="vgStep2" ControlToValidate="txtMobilePhone" CssClass="error"></asp:CustomValidator>
                        <asp:RequiredFieldValidator ID="rfvMobilePhone" runat="server" ControlToValidate="txtMobilePhone"
                            ErrorMessage="Mobile Phone is required." CssClass="error" ValidationGroup="vgStep2"></asp:RequiredFieldValidator>
                        <asp:CustomValidator ID="cvMobilePerVal" runat="server" ClientValidationFunction="validateEmailOrMobilePerVal"
                            Display="Dynamic" ValidationGroup="vgStep2"></asp:CustomValidator>


 <asp:Button ID="btnStep2Upper" runat="server" ClientIDMode="Static" OnClick="btnSaveContactClick" Text="Save" ValidationGroup="vgStep2" vg="vgStep2" OnClientClick="return ClientValidate();" />

服务器端代码

    protected void cvMobilePhone_ServerValidate(object source, ServerValidateEventArgs value)
        {  /* I have put debugger here but control is not coming here*/
            /* my validation logic*/
protected void cvMobilePhone_ServerValidate(object source, ServerValidateEventArgs value)
    {
        if (txtMobilePhone.Text.Trim() != "")
        {
            RewardProgramDataContext db = new RewardProgramDataContext();
            Boolean f = false;
            string MobilePhone = cmnFunc.RemoveMobilePhoneFormat(txtMobilePhone.Text.Trim());
            if (Request["id"] != null)
            {
                var cData = db.spContactSelectAllSingle(new Guid(Request["id"])).SingleOrDefault();
                if (cData != null)
                {
                    if (cmnFunc.RemoveMobilePhoneFormat(cData.MobilePhone) == MobilePhone)
                    {
                        f = true;
                        value.IsValid = true;
                    }
                }
            }
            if (f == false)
            {
                var res = db.spContactDuplicateMobile(new Guid(ddlContactList.SelectedValue), MobilePhone).SingleOrDefault();
                if (res.Column1 <= 0)
                {
                    value.IsValid = true;
                    customIsValid = true;
                }
                else
                {
                    value.IsValid = false;
                    customIsValid = false;
                }
            }
        }
    }

现在,当我点击提交按钮时,所有灵活的边验证工作,但是serside自定义验证器没有调用

2 个答案:

答案 0 :(得分:0)

您忘了设置ControlToValidate属性?

<asp:CustomValidator ID="cvMobilePhone" runat="server" ControlToValidate="txtMobilePhone" OnServerValidate="cvMobilePhone_ServerValidate" 
                            Text="Mobile Phone already exist in this Reward Program." ErrorMessage="Mobile Phone already exist in this Reward Program."
                            Display="Dynamic" ValidationGroup="vgStep2" CssClass="error"></asp:CustomValidator>

答案 1 :(得分:0)

您有两种不同的组合导致此行为。

首先请注意,尽管 - 正如其他人所说的那样 - 您不必指定ControlToValidate,这样做会限制服务器端自定义验证事件将触发的环境。具体来说,如果您将其设置为未设置,则事件始终在回发时触发,而如果您设置该事件,则当ControlToValidate标识的控件具有非空值时,事件仅触发。 / p>

其次,通过指定OnClientClick,您告诉框架您将负责客户端验证,除非您从OnClientClick函数调用它,否则现在不会触发。虽然您没有在问题中包含ClientValidate函数,但我怀疑您没有这样做,这使您的RequiredFieldValidator无法阻止回发。

结合起来,这两件事意味着

  • 尽管是空文本框,但
  • 仍然会发生回发
  • 由于空文本框,服务器端自定义验证不会在回发时触发,

您可以使用Page_ClientValidate())从自定义函数调用客户端验证,该页面脚本中将包含验证程序。

function ClientValidate() {
    if (Page_ClientValidate()) {
        //do custom validation, maybe return false 
        return true;
    }
    else {
        return false;
    }

}