简体   繁体   English

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

[英]ASP.NET server side validation not firing

In My Application server side validation function is not working.even function is not called. 在我的应用程序服务器端验证功能不起作用,甚至没有调用该功能。 i have put debugger on thuat function but it is not stopped ny debugger .ie function is not called 我已经将调试器放在thuat函数上,但是它并未停止ny debugger .ie函数未调用

<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();" />

Server Side Code 服务器端代码

    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;
                }
            }
        }
    }

now when i click submit button all clent side validation working but serside custom validator is not calling 现在,当我单击提交按钮时,所有clent方面的验证都有效,但是serside自定义验证程序未调用

You forget to set the ControlToValidate property? 您忘记设置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>

You have a combination of two different things causing this behaviour. 您将两种不同的因素结合在一起导致此行为。

Firstly, note that although—as has been said by others—you do not have to specify ControlToValidate , doing so restricts the circumstances in which the server-side custom validation event will fire. 首先,请注意,尽管(正如其他人所说的那样)您不必指定ControlToValidate ,但是这样做限制了服务器端自定义验证事件将触发的情况。 Specifically, if you leave it unset, the event always fires on postback, whereas if you set it, the event only fires when the control identified by ControlToValidate has a non-empty value . 具体来说,如果您将其保留为未设置状态,则该事件始终在回发时触发,而如果您将其设置,则仅ControlToValidate标识的ControlToValidate具有非空值时才触发该事件。

Secondly, by specifying OnClientClick, you are telling the framework that you will take care of client-side validation, which will now not fire unless you call it from your OnClientClick function. 其次,通过指定OnClientClick,您将告诉框架您将负责客户端验证,除非您从OnClientClick函数调用它,否则现在不会触发。 Although you have not included your ClientValidate function in your question, I suspect you are not doing so, which leaves your RequiredFieldValidator powerless to prevent the postback. 尽管您的问题中没有包含ClientValidate函数,但我怀疑您没有这样做,这使您的RequiredFieldValidator无法阻止回发。

In combination, these two things mean that 结合起来,这两件事意味着

  • the postback occurs despite the empty textbox, and 尽管文本框为空,也会发生回发,并且
  • the server-side custom validation does not fire on postback, because of the empty textbox. 由于文本框为空, 因此服务器端的自定义验证不会在回发时触发。

You can call the client validation from your custom function using Page_ClientValidate()) , which will be present in your page script since the page contains validators. 您可以使用Page_ClientValidate())从自定义函数调用客户端验证,由于页面包含验证器,该验证将出现在页面脚本中。

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

} 

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

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