Wookie-o
Wookie-o

Reputation:

ASP.NET MVC ViewUserControl: How do I load its scripts dynamically?

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.

Does anyone have any ideas?

Upvotes: 1

Views: 1142

Answers (1)

David
David

Reputation: 3365

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

How to best control loading of multiple javascript files in ASP.NET?

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

Upvotes: 1

Related Questions