views:

584

answers:

2

I have a ViewUserControl that will be used in some pages in my site, but not all. This ViewUserControl requires a javascript file, so I would like to have the script reference added automatically to the head session of the pages that are using this ViewUserControl, is that possible? I tried to add a content control to it, but looks like it is not allowed.

I could also add the script tag straight into the ViewUserControl's .ascx code, but then I will have the script reference added N times if I have N of such controls in a page.

Any ideas?

A: 

A quick google gave this solution from a stackoverflow post:

Content place holder in the head of the master page.

Cohen
+1  A: 

In the code-behind of the ASCX, register your script reference (or block). I just answered a similar question explaining how to do this:

http://stackoverflow.com/questions/732854/how-to-best-control-loading-of-multiple-javascript-files-in-asp-net/733411#733411

With this technique, you can have the same control on the page multiple times, yet still include the script only once (and only on pages that actually include the control).

David