如何控制它的JavaScript得到的UpdatePanel后部分回发endRequest运行? [英] How to control which JavaScript gets run after UpdatePanel partial postback endRequest?

查看:177
本文介绍了如何控制它的JavaScript得到的UpdatePanel后部分回发endRequest运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以连接到客户端的事件各部分的回发后运行JavaScript;然而,我想要做这样的事情:

I know I can hook into the client side events to run JavaScript after every partial postback; however, I want to do something like this:

protected void FooClicked(object sender, EventArgs e) {
        ClientScript.RegisterStartupScript(GetType(), "msg", "showMsg('Foo clicked');",true);        
}

我知道我可以完全隐藏字段本事后运行的东西的每个的回传,但应该有一个pretty straightfoward方式以类似的方式了这一点。

I know I could totally hack it with hidden fields and run something after every postback, but there should be a pretty straightfoward way to in a similar fashion to this.

推荐答案

您所描述的不符合的部分后背上,工作的具体code样品,因为 ClientScript.RegisterStartupScript()写JS来在请求生命周期的输出施工阶段的页面;而部分回发仅更新通过JavaScript(即使服务器上生成整个页面,包括你的启动脚本标记)。

The specific code sample you are describing does not work with partial post-backs, since ClientScript.RegisterStartupScript() writes JS to the page during the output construction phase of the request lifecycle; whereas a partial postback only updates a selected portion of the page via JavaScript (even though the markup for the entire page, including your startup script, is generated on the server).

要紧密地模仿你所描述的,你应该包括你的UpdatePanel内立即控制,和部分回发期间设置您想运行的内容面板到脚本的Text属性:

To closely mimic what you are describing, you ought to include a Literal control inside your UpdatePanel, and during partial postback set the Text property of the content panel to the script you wish to run:

myLiteral.Text = "<script type=\"JavaScript\">doStuff();</script>";

海事组织,一个更合适的方式是异步回发来使用客户端API注册一个事件处理程序来运行回发完成时:

IMO, a more proper way is to use the client-side API for async postbacks to register an event handler to run when the postback completes:

function endRequestHandler(sender, args) {
    doStuff();
}

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);

如果您需要传递这是回发到处理过程中产生的信息,您可以通过它通过隐藏字段,抓住从您的客户端处理程序的DOM。

If you need to pass information which was generated during the postback into the handler, you can pass that via hidden fields and grab that from the DOM in your client-side handler.

这篇关于如何控制它的JavaScript得到的UpdatePanel后部分回发endRequest运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆