Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net ensure javascript is loaded once only

I've been working in asp.net webforms and I've been making a UserControl that depends on a small bit of javascript which is in an external file.

I want to be able to put a reference to the javascript inside the UserControl to ensure that it gets loaded on the page, but the problem is that the UserControl can appear multiple times on a page so I only want the script to be loaded with the first instance of the UserControl.

Is there an easy way to do this in ASP.NET Webforms?

like image 257
lomaxx Avatar asked Jan 06 '10 23:01

lomaxx


1 Answers

Use the ClientScriptManager's RegisterClientScript() methods to handle this. In your case, since you're including the script from an external file, use RegisterClientScriptInclude().

Alternately, if you're using UpdatePanels, use ScriptManager's equivalent Register...() methods.

like image 122
womp Avatar answered Oct 03 '22 21:10

womp