views:

164

answers:

1

Hey,

I've spent at least 2 days trying anything and googling this...but for some reason I can't get RegisterClientScriptInclude to work the way everyone else has it working?

First off, I am usting .NET 3.5 Ajax,...and I am including javascript in my partial page refreshes...using this code:

ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "MyClientCode", script, true);

It works perfectly, my javascript code contained in the script variable is included every partial refresh.

The javascript in script is actually quite extensive though, and I would like to store it in a .js file,..so logically I make a .js file and try to include it using RegisterClientScriptInclude ...however i can't for the life of my get this to work. here's the exact code:

ScriptManager.RegisterClientScriptInclude(this, typeof(Page), "mytestscript", "/js/testscript.js");

the testscript.js file is only included in FULL page refreshes...ie. when I load the page, or do a full postback....i can't get the file to be included in partial refreshes...have no idea why..when viewing the ajax POST in firebug I don't see a difference whether I include the file or not....

both of the ScriptManager Includes are being ran from the exact same place in "Page_Load"...so they should execute every partial refresh (but only the ScriptBlock does).

anyways,..any help or ideas,..or further ways I can trouble shoot this problem, would be appreciated.

Thanks, Andrew

+1  A: 

Here's the key:

partial page refreshes

You have to jump through special hoops to add javascript to a page after the initial load, because loading javascript later is considered a security risk by some (it's also bad for Google indexing).

Instead, register the scripts on the initial page load and just don't execute the scripts until later. If these scripts are created dynamically, I suggest you factor out the static portion and refactor as methods you can call with information returned dynamically from your page refresh.

Joel Coehoorn
Thanks,The problem with registering the script initially then executing it later is that my script has event listeners in it. So if I register a script listening for a click event inside some div inside my updatepanel....then my updatepanel get's refreshed...I have to reload the listener using RegisterClientScriptBlock for it to work.....i just don't want javascript code in my codebehind....
Andrew
The .js files really should only be declaring methods you can later call from your own code (In this case most likely using RegisterClientScriptBlock). You're trying to use include files like function calls and that's why you're having problems, it's just not how they should be used.
Tim Schneider