繁体   English   中英

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

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

我有以下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>

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

还有以下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;
        }
    }
}

在控件所在的ASPX页面中,我还具有以下全局变量定义:

 <script type="text/javascript">

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

我的问题是我在服务器端函数上放置了一个断点,每次单击提交按钮时,都会调用服务器端代码,并且页面会刷新。 如果我在chrome中调试,则可以看到我所有的JS变量都具有期望值,这似乎出于某种原因似乎客户端JS函数未触发,而是移交给了服务器端函数。

正如@ Justin-iurman所指出的,这里发生的事情是,当函数返回true ,服务器端函数也会在客户端之后被调用。 就我而言,当我测试false结果时,我仍然直接使用服务器端功能。

事实证明,通过按Chrome中的F12键并检查控制台,我的JS代码出现了未定义的错误。 将有问题的功能从该页面专用的功能中移出,并移到全局上下文中对我来说是个问题。

暂无
暂无

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

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