C#的UpdatePanel更新后更新的JavaScript [英] Update JavaScript after C# UpdatePanel update

查看:199
本文介绍了C#的UpdatePanel更新后更新的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JavaScript的执行它在页面加载动态创建的块。该JS是一个UpdatePanel的里面。

I have a dynamically created block of JavaScript which executes on page load. The JS is inside a UpdatePanel.

在UpdatePanel的更新,我需要更新我的动态创建的JavaScript。这一切工作正常,当我查看该页面的生成的源,我可以看到更新的JS。

When the UpdatePanel is updated, I need to update my dynamically created JavaScript. This all works fine as when I view the generated source of the page, I can see the updated JS.

我的问题是,在该页面的更新,我需要手动重新执行JavaScript函数(因为这是自动在页面加载正常完成)。

My problem is that after the page is updated, I need to re-execute the JavaScript function manually (as this is normally done automatically on page load).

我已经尝试了多种方法:

I've tried a number of methods:

Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoaded);

function pageLoaded(sender, args) {
    var updatedPanels = args.get_panelsUpdated();

    // check if Main Panel was updated 
    for (idx = 0; idx < updatedPanels.length; idx++) {
        if (updatedPanels[idx].id == "PageContent_TabContent_UpdatePanel1") {
            drawVisualization();
            break;
        }
    }
}

或者

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

function handleEndRequest(sender, args) {
    drawVisualization();
}

不过,上述两种方法只是执行旧版本的JavaScript中,根据生成的源$ C ​​$ C,其中,已经改变了。

But both of the above methods just execute the old version of the JavaScript, which according to the generated source code, has changed.

有我丢失的东西,或者是另一种方法,我可以尝试?

Is there something I'm missing, or maybe another method I could try?

更新:

使用注释@Mehdi Maujood,我已经把工作解决方案:

Using the comment @Mehdi Maujood, I've put together a working solution:

function handleEndRequest(sender, args)
{
    var updatedPanels = args.get_panelsUpdated();
    for (idx = 0; idx < updatedPanels.length; idx++) {
        if (updatedPanels[idx].id == "PageContent_TabContent_UpdatePanel1") {

            var scripts = updatedPanels[idx].getElementsByTagName('script');
            for(var i=0;i<scripts.length;i++) {
                eval(scripts[i].innerHTML);
            }

            drawVisualization();
            break;
        }
    }
}

Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(handleEndRequest);

该组合的答案从<一个href="http://stackoverflow.com/questions/1625791/how-can-i-recall-a-javascript-function-after-updatepanel-update">How我记得UpdatePanel的更新后的JavaScript函数以及对<一个@ shatl的评论href="http://stackoverflow.com/questions/301473/rebinding-events-in-jquery-after-ajax-update-updatepanel">Rebinding jQuery中的事件的Ajax更新(的UpdatePanel)后。

This combines answers from How can I recall a javascript function after updatepanel update as well as @shatl's comment on Rebinding events in jQuery after Ajax update (updatepanel).

推荐答案

据我可以理解,您遇到的问题是,通过更新面板呈现的JavaScript是没有得到执行。我搜索了一下,这似乎是常见的问题。浏览器只是似乎没有执行脚本包在一个更新面板。

As far as I can understand, the problem you're having is that the javascript rendered via the update panel is not getting executed. I searched a bit, and this appears to be common problem. The browser just doesn't seem to execute script wrapped in an update panel.

问题是,虽然更新的脚本发送到浏览器,它永远不会执行。 drawVisualization从未重新定义。你可以尝试这样的事情从C#code:

The issue is that although your updated script is sent to the browser, it is never executed. drawVisualization is never re-defined. You can try something like this from your C# code:

string script = "function drawVisualization() { alert('Drawing code here') }";

ScriptManager.RegisterClientScriptBlock(this.Page, typeof(SomeClass), Guid.NewGuid().ToString(), script, true);

要的RegisterClientScriptBlock通话可能需要固定的一些参数;我刚刚从什么地方抄了一遍。

The call to RegisterClientScriptBlock may need fixing for some parameters; I just copied it from somewhere.

编辑:我发现了一个更好的办法来从另一个问题,做到这一点。请参见注释。这里有一张$ C,它执行按更新面板呈现脚本$ C(可以忽略previous code):

I found a better way to do this from another question. Refer to the comment. Here's a piece of code that executes scripts rendered by update panels (you can ignore the previous code):

<script type="text/javascript">
    function handleEndRequest(sender, args)
    {
        var panel = document.getElementById(sender._postBackSettings.panelID);
        var scripts = panel.getElementsByTagName('script');
        for(var i=0;i<scripts.length;i++) {
            eval(scripts[i].innerHTML);
        }
    }

    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(handleEndRequest);
</script>

这篇关于C#的UpdatePanel更新后更新的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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