异步回发不引起的document.ready被执行 [英] Async postback doesn't cause document.ready to be executed

查看:192
本文介绍了异步回发不引起的document.ready被执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不实施一些更改,这些更改是在一个几页使用的用户控件。用户控件包含一些jQuery来处理寻呼任务(显示屏3个月的数据,并在同一时间隐藏9)。当加载控件,它会自动显示当前季度和$执行该code(文件)。就绪()。

I've had to implement some changes to a user control that's used in a couple of pages. The user control contains some JQuery to handle a paging task (displays 3 months of data and hides 9 at a time). When the control is loaded, it will automatically display the current quarter and executes this code in $(document).ready().

我的问题是,在使用该用户控件的网页中的一个,该控制是不在页加载可见。一个异步回发是用来改变的知名度,但这不执行就绪()。

The problem I have is that in one of the pages the user control is used, the control isn't visible on page load. An async postback is used to change the visibility but this doesn't execute ready().

我发现了一个片段,它允许托管页面截取部分回发的EndResponse但我仍然不能在用户控件中执行的功能。

I found a snippet which allows the hosting page to intercept the EndResponse of the partial postback but I still can't execute the function within the usercontrol.

有没有人有什么建议吗?

Does anyone have any suggestions?

干杯

戴夫

推荐答案

像我一样,你会恨prescribed微软的答案。在prescribed的答案是使用PageRequestManager设置一个请求处理程序。这个请求处理程序(当时)执行的每个局部回传完成后。

As I did, you're going to hate the prescribed Microsoft answer. The "prescribed" answer is to use the PageRequestManager to setup a request handler. This request handler is (then) executed after each partial-postback is completed.

请求处理程序示例:

<script id="events" type="text/javascript">

    jQuery(document).ready(function() {

        // Your normal code goes here
        setupSomething();
        initializeSomethingElse();

        // Setup your partial-postback event handler.
        // This is used to rewire all events since all are 'lost' after partial-postback.
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(requestHandler);
    });

    ///<summary>partial postback event handler. Executed after the partial postback is completed. Clears modal popup textboxes</summary>
    ///<param name="sender"></param>
    ///<param name="args">http://www.asp.net/ajax/documentation/live/ClientReference/Sys.WebForms/EndRequestEventArgsClass/default.aspx</param>
    function requestHandler(sender, args) {

        if (args.get_error() == undefined) {

            // Your normal code goes here
            setupSomething();
            initializeSomethingElse();
        }
        else
            alert(args.get_error()); // Do something
    }
</script>

这给我们带来了答案很简单:

为什么不从code-背后明确初始化用户控制,并保持你的用户控件的HTML中的JavaScript初始化(本身)。

That Brings Us To The Simple Answer:
Why not initialize your user-control explicitly from your code-behind and keep that initializing JavaScript within your user-controls HTML (itself).

void YourUserControl_PreRender(object sender, EventArgs e)
{
    try
    {

    }
    catch (Exception ex)
    {

    }
    finally 
    {
        // Do this
        ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "registerInitializer", buildInitializer(), true);
    }
}

在呈现时,buildInitializer从逻辑上说,如果客户端上存在此功能......调用它。而且......它的作品每一次。

Once rendered, the "buildInitializer" logic says, "If this function exists on the client...call it." And...it works every time.

private string buildInitializer()
{
    StringBuilder javascript = new StringBuilder();

    javascript.Append("if (window.initializeMyControl) {");
    javascript.Append("if(typeof window.initializeMyControl == 'function') { initializeMyControl(); }");
    javascript.Append("}");

    return javascript.ToString();
}

您现在的用户控件初始化可以住在用户控件,它应该是:

<script type="text/javascript">
    function initializeMyControl() {

        // Your normal code goes here
        setupSomething();
        initializeSomethingElse();
    }
</script>

这篇关于异步回发不引起的document.ready被执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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