how can i call a javascript function when an ASP.net validation summary is filled

白昼怎懂夜的黑 提交于 2019-12-11 02:08:46

问题


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?


回答1:


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



回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/3060220/how-can-i-call-a-javascript-function-when-an-asp-net-validation-summary-is-fille

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!