简体   繁体   English

如何从后台代码执行javascript函数,但仅在页面加载后执行

[英]How to execute javascript function from code-behind but only after the page loads

I've found other solution on how to executes a javascript function from code-behind. 我已经找到了关于如何从后台代码执行javascript函数的其他解决方案。 The problem is that it firing before the page loads, so I can't add logic to it. 问题在于它在页面加载之前触发,因此我无法为其添加逻辑。

if(result)
{
   Page.ClientScript.RegisterClientScriptBlock(typeof(Page), 
                                               "Script", "confirmPwdChange();", true);
}

On the client side 在客户端

<script>
   confirmPwdChange()
   {
     alert("Password has been successfully changed.");
     //Add more logic here, after user clicks the OK button...
   }
</script>

No Jquery, if possible. 如果可能,没有Jquery。

Thanks for helping. 感谢您的帮助。

I think you're looking for PageRequestManager 我认为您在寻找PageRequestManager

List of PageRequestManager events PageRequestManager事件列表

var prm = Sys.WebForms.PageRequestManager.getInstance();

// beginRequest: before postback to server
prm.add_beginRequest(BeginRequestHandler);

// endRequest: after postback to server
prm.add_endRequest(EndRequestHandler);

function BeginRequestHandler(sender, args) {
    // do stuff
}

function EndRequestHandler(sender, args) {
    // do stuff
}

You might want to include the window.onload in your script file. 您可能希望在脚本文件中包含window.onload。 something like this.. 像这样

StringBuilder script = new StringBuilder();
script.Append("var existingHandler = window.onload;");
script.Append("window.document.body.onload = function(){ confirmPwdChange(); if (existingHandler){ existingHandler()}}");
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), 
                                           "Script", script.ToString(), true);

this ensure, your change password is called on page load and retains any other existing handlers declared already on the page. 这样可以确保在页面加载时调用您的更改密码,并保留页面上已经声明的任何其他现有处理程序。

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

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