views:

139

answers:

1

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

A: 

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

Sean Kinsey
Hmm, it actually seems like you are correct. When I digged in to it I actually realized that I used the Page instead of the usercontrol to register the script which was mistake number one. Mistake number two is that i included an add_endRequest in my script whose intension was to handle async postbacks, but this is not needed because it seems like ScripManager does this for me, hence the duplicate calls.Thanks for the answer though :)
Andreas