简体   繁体   English

JavaScript客户端功能未触发ASP自定义验证器

[英]JavaScript client side function not firing for ASP custom validator

I have the following ASP CustomValidator: 我有以下ASP CustomValidator:

<asp:CustomValidator ID="CustomValidator2" runat="server"
    EnableClientScript="true" OnServerValidate="ins_server"  ClientValidationFunction ="ins_client" 
    ErrorMessage="CustomValidator"> * Insurance dates not valid without supporting attached document</asp:CustomValidator>

The following C# server side function: 以下C#服务器端功能:

protected void ins_server(object source, ServerValidateEventArgs args)
{
    //new user
    if (PageMode == PageModes.NewVessel)
    {
        if (fuAttachment.HasFile && datetimepickerinsend != null && datetimepickerinsstart != null)
        {
            args.IsValid = true;
        }              

        else
        {
            args.IsValid = false;
        }
    }    

    //existing user
    if (PageMode == PageModes.EditVessel)
    {
        args.IsValid = true;
    }
}

And also the following client side function in JavaScript stored in a file named customfunctions.js: 还有以下JavaScript中的客户端功能存储在名为customfunctions.js的文件中:

//declerations
var insurancestart;
var insuranceend;
var filesattached;

function ins_client(sender, e) {
    if (pagemode == 'EditVessel') {
        e.IsValid = true;
    }

    if (pagemode == 'NewVessel') {
        if (insurancestart !== '' && insuranceend !== '' && filesattached > 0) {
            e.IsValid = true;
        }
        else {
            e.IsValid = false;
        }
    }
}

I also have the following global variable definition in the ASPX page the control sits on: 在控件所在的ASPX页面中,我还具有以下全局变量定义:

 <script type="text/javascript">

     var pagemode;
     $(document).ready(function () {
         // removed var to give global scope
         pagemode = '<%=this.PageMode.ToString()%>';                         
     });
</script>

My problem is I have placed a breakpoint on the server side function, every time I click the submit button the server side code is being called and the page refreshes. 我的问题是我在服务器端函数上放置了一个断点,每次单击提交按钮时,都会调用服务器端代码,并且页面会刷新。 If I debug in chrome I can see that all my JS variables have the expected values, it just seems like for some reason the client side JS function is not firing and is instead falling over to the server side function. 如果我在chrome中调试,则可以看到我所有的JS变量都具有期望值,这似乎出于某种原因似乎客户端JS函数未触发,而是移交给了服务器端函数。

What was happening here was as @Justin-iurman pointed out, when the function returns true the server side function will also be called after the client. 正如@ Justin-iurman所指出的,这里发生的事情是,当函数返回true ,服务器端函数也会在客户端之后被调用。 In my case though when I tested a false result I was still going straight to the server side function. 就我而言,当我测试false结果时,我仍然直接使用服务器端功能。

Turns out by pressing F12 in chrome and inspecting the console, I had an undefined error in my JS code. 事实证明,通过按Chrome中的F12键并检查控制台,我的JS代码出现了未定义的错误。 Moving the function in question out of the function used specifically for this page and into global context sorted the issue for me. 将有问题的功能从该页面专用的功能中移出,并移到全局上下文中对我来说是个问题。

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

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