ScriptManager duplicates javascript

Posted by Andreas on Stack Overflow See other posts from Stack Overflow or by Andreas
Published on 2010-05-28T10:37:51Z Indexed on 2010/05/28 10:42 UTC
Read the original article Hit count: 247

Hi!

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 asyncronous 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 asyncronous 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.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about asp.net-ajax