简体   繁体   English

服务器端验证完成后运行javascript函数

[英]Run javascript function after Server-Side validation is complete

Ok, I've got a lightbox with a small form (2 fields) in it, inside an UpdatePanel, and I want to close this lightbox (must be done via javascript) when the 'Save' button is pressed. 好的,我在UpdatePanel中有一个带有小表格(2个字段)的灯箱,当我按下“保存”按钮时,我想关闭此灯箱(必须通过javascript完成)。

However, there is a need to have a server-side CustomValidator on the page, and I only want to close the lightbox if this returns as valid. 但是,页面上需要有一个服务器端CustomValidator,如果此灯箱返回有效值,我只想关闭灯箱。

Does anyone know a way to trigger javascript (or jQuery) code from a server-side validator? 有谁知道从服务器端验证器触发javascript(或jQuery)代码的方法吗?

You can add a little snippet of code using the ScriptManager to execute after the response comes back to the UpdatePanel . 您可以使用ScriptManager添加一些代码段,以在响应返回到UpdatePanel之后执行。

if (Page.IsValid){

   ScriptManager.RegisterStartupScript(
            customValidator1, 
            typeof(MyPageClass), 
            "closeBox",
            "myLightBoxVariableOnThePage.close()",
            true);
}

When that server side validator runs, it will send a whole new page to the browser. 该服务器端验证程序运行时,它将向浏览器发送一个全新的页面 Anything that was shown in the browser before was destroyed , including any state kept in your javascript. 之前在浏览器中显示的所有内容均已销毁 ,包括javascript中保留的所有状态。 If new page bears a strong resemblance to the old page, you should consider this a happy coincidence. 如果新页面与旧页面非常相似,则应将其视为一个巧合。

Therefore, the thing to do here is rather than executing a javascript function, have your CustomValidator make the correct changes to the page on success so that it's rendered to the browser correctly in the first place. 因此,要做的事情不是执行javascript函数,而是让CustomValidator成功完成对页面的正确更改,从而首先将其正确呈现给浏览器。

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

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