开发者

ScriptManager duplicates javascript

开发者 https://www.devze.com 2023-01-01 23:13 出处:网络
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 ite

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 th开发者_Python百科e 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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消