当 ASP.net 验证摘要已填写时,如何调用 javascript 函数

发布于 2024-09-06 04:03:29 字数 145 浏览 2 评论 0原文

我的页面中有一个validationSummary。我想在validationSummary填充后调用一个javascript函数。我怎样才能做到这一点?

我认为我应该在后面的代码中添加一个属性,但我无法弄清楚该属性的键是什么。

有什么帮助吗?

i have a validationSummary in my page. i want to call a javascript function after the validationSummary is filled. how can i achieve this?

i think i should add an attribute in the code behind, but i can't figure out what is the attribute's key.

any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贩梦商人 2024-09-13 04:03:29

您需要调用 JavaScript 函数 Page_ClientValidate() 来启动连接到页面控件的 ASP.NET 验证。调用此函数后,Page_IsValid 布尔值将被正确设置。

这是我如何使用它的示例。如果 ASP.NET 验证成功,我将禁用该按钮,否则该按钮保持启用状态并向用户显示验证摘要。 OnClientClick 来自 ASP:Button 控件。

OnClientClick="javascript:Page_ClientValidate(); if (Page_IsValid==true) { this.disabled=true; }"

You will want to call the javascript function Page_ClientValidate() to initiate the ASP.NET validation wired to your page controls. Once this is called, the Page_IsValid boolean value will be properly set.

Here is an example of how I am using it. If ASP.NET validation is successful, I disable the button, otherwise the button remains enabled and the validation summary is displayed to the user. The OnClientClick is from a ASP:Button control.

OnClientClick="javascript:Page_ClientValidate(); if (Page_IsValid==true) { this.disabled=true; }"
蘑菇王子 2024-09-13 04:03:29

我认为这是不可能的。但是,可以通过在执行回发的控件上设置 OnClientClick 来拦截验证。然后您可以检查全局 JavaScript 变量 Page_IsValid 来获取验证结果。

需要记住的一件事是,当页面上没有验证器时,Page_IsValid 将是未定义的。

I don't think that's possible. It is, however, possible to intercept validation by setting OnClientClick on the controls that do postbacks. Then you can check the global JavaScript variable Page_IsValid for the validation result.

One thing to keep in mind is that, when there are no validators on a page, Page_IsValid will be undefined.

空心空情空意 2024-09-13 04:03:29

我开始通过 @EverettEvola 实现该解决方案,但也多次调用验证逻辑并显示多个 ValidationSummary 弹出窗口。我的解决方案如下:

在按钮上(在我的例子中,按钮是提交按钮)

OnClientClick="return CustomValidationOnClick()"

和 CustomValidationOnClick()

function CustomValidationOnClick(source, args) {

    //Manually kickoff page validation
    //This call will display the Validation summary popup if page is invalid
    Page_ClientValidate();

    //Page_IsValid set by the result of the Page_ClientValidate() call
    if (Page_IsValid == true) {
        this.disabled=true;
        return true; //if Submit button return true to continue form submit
    }
    else {
       //do whatever here
       return false; //if Submit button return false to cancel form submit
    }
}

I started to implement the solution by @EverettEvola but also where the validation logic was being called multiple times and displaying multiple ValidationSummary popups. My solution was as follows:

On the button (in my case the button was a submit button)

OnClientClick="return CustomValidationOnClick()"

And the CustomValidationOnClick()

function CustomValidationOnClick(source, args) {

    //Manually kickoff page validation
    //This call will display the Validation summary popup if page is invalid
    Page_ClientValidate();

    //Page_IsValid set by the result of the Page_ClientValidate() call
    if (Page_IsValid == true) {
        this.disabled=true;
        return true; //if Submit button return true to continue form submit
    }
    else {
       //do whatever here
       return false; //if Submit button return false to cancel form submit
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文