One of the problem areas of writing custom web control is the dependency you can get when referencing script files (such as javascript).
Another problem is ensuring that you always use the latest version of that file as well.
so how to overcome this? simple, add the javascript file as a resource and load it into the ScriptManager as a script reference.\
When you’ve written the javascript, in your control library project, simply go to your AssemblyInfo.cs file and add the following line:
assembly: WebResource(“TestControl.Scripts.TestScript.js”, “text/javascript”)]
Where “TestScript.js” is the reference to the script file.
In your control’s OnPreRender method you locate the ScriptManager in the current page and add the script reference:
protected override void OnPreRender(EventArgs e)
{
var scriptManager = ScriptManager.GetCurrent(this.Page);
if (scriptManager != null)
{
scriptManager.Scripts.Add(
new ScriptReference
{
Assembly = “TestControl.Scripts”
, Name = “TestControl.Scripts.TestScript.js”
, IgnoreScriptPath = true
}
);
}
base.OnPreRender(e);
}
Now you can easily reference functions within the javascript file or even run scripts, set up events etc, such as AJAX events.
Last step is simply to reference the assembly (TestControl.dll) in your ASP.Net project/Web Application and include the custom control in your page – voila..simple.
source :: brianmadsen
payam

