ScriptManager duplicates javascript

点点圈 提交于 2019-12-12 03:54:14

问题


I have a usercontrol that can be used in for example a gridview itemtemplate, this means that the control might or might not be on the page at page load. In the case where the control is inside an itemtemplate i will popupate the gridview via asynchronous postbacks (via updatepanels).

The control itselfs registrers scriptblocks since it is depending on javascripts. First i used

Page.ClientScript.RegistrerClientScriptBlock

But this doesn't work on asynchronous postbacks (updatepanels) so i then tried the same using ScriptManager which allows me to registrer scripts on the page after async postbacks. great!.

ScriptManager.RegisterClientScriptBlock

However, ScriptManager (what i know of) does not have the functionallity to see if a script already is on the page, so i will for every postback generate duplicates of the script blocks, this is ofcourse unwanted behaviour.

I did a run at Google and found that i can call the Dispose() method of the PageRequestManager can be used, this does work since it clears the scripts and then adding them again (this also solves my issue with removing unused script blocks from removed controls).

Sys.WebForms.PageRequestManager.getInstance().Dispose()

However, ofcourse there is a downside since im posting here :). The Dispose() method disposes the instance on the master page as well which leads to scripts running there will stop to function after an async postback (updateprogress for example).

So, is there a way to check if a script already exists on the page using ScriptManager or any other tools, that will prevent me of inserting duplicate scripts? Also, is there a way to remove certain script blocks (when i am removing an item in itemtemplate for example).

Big thanks in advance.


回答1:


If you set the same type and key attributes when registering then I think the SM will include only one of these.




回答2:


Try a function like this one:

Public Sub AddScriptToCompositeScriptSafety(ByRef manager As ScriptManager, ByRef script As ScriptReference)

    For Each item In manager.CompositeScript.Scripts
        If (item.Path = script.Path) Then
            Return
        End If
    Next
    manager.CompositeScript.Scripts.Add(script)

End Sub


来源:https://stackoverflow.com/questions/2928263/scriptmanager-duplicates-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!