I'm creating my first ASP.NET server control based on a jQuery-enhanced textbox I found online. Part of the original code includes a jQuery extension file, so I need to include that with my control. (The base jQuery library is a requirement of the web application so for our purposes here we can safely assume it's already available on any page the control is used.)
I first added the javascript file as a resource and was able to write the stream in the control's Render event. While it created the HTML/javascript as wanted, the jQuery doesn't work quite right.
The second problem I have is I anticipate several instances of the control to be used on any given page, so even if the above method worked, that javascript would be constantly repeated. Yuck.
How can I include this javascript with my control but ensure only a single iteration of it is rendered? Googling has found some references to using as [assembly]
attribute and Page.RegisterClientScriptResource
, but I'm not too clear on that.
Page.ClientScript.RegisterClientScriptResource(getType(page), _
page.ResolveUrl("~/scripts/jquery.js")
The entire point of the RegisterClientScriptResource is to prevent the page from reloading a duplicate script.
Note that the above example assumes you are working on an Intranet. If you are working with a public website, it is better to use CDN Hosted file (http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js)