I've created a ASP.Net Server Control, lets call it "WebGrid". The WebGrid control has an embedded javascript resource that it registers with the ScriptManager of whatever page is hosting it.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
ClientScriptManager scriptManager = this.Page.ClientScript;
scriptManager.RegisterClientScriptResource(typeof(WebGrid), "Atl.Core.Resources.Scripts.WebGrid.js");
}
I have also made sure that the WebGrid.js file is an embedded resource by changing the build action property of the file to "Embedded Resource" and added the resource to the AssemblyInfo.cs of the project as such:
[assembly: WebResource("Atl.Core.Resources.Scripts.WebGrid.js", "text/js")]
Now, when the control is declared in the markup of an aspx page the javascript resource is registered with the ScriptManager and everything works fine. BUT, if I add the control to the Page's ControlCollection dynamically through a button click the javascript does NOT get registered and the control complains cannot find such and such javascript function. Note both the button and the WebGrid are located in different UpdatePanels...
EDIT: Looks like the problem is because I am using the ClientScript vs. ScriptManager see(Differences between ScriptManager and ClientScript when used to execute JS?). So, how do I then register the WebGrid's script with the Page's ScriptManager?
So I figured it out. The answer is pretty simple. In the control PreRender event handler just call the static RegisterClientScriptResource method on the ScriptManager class. I was confused because I thought I needed to reference the physical ScriptManager on the page! Nope. You still need to set the script as an embedded resource and add the WebResource attribute in the Assembly.cs file.
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
ScriptManager.RegisterClientScriptResource(this, typeof(ScriptTest), "Atl.Core.Resources.Scripts.ScriptTest.js");
}