I'm creating a custom script control in ASP.NET
The purpose of the control is simply a server variant of the tag, used to load javascript files The main purpose of this control however is to combine multiple scripts into one response so on the client side they see something like tag for each location, so all scripts registered in the DocumentTop location will be combined into a single tag with the exception of the location "inline", all inline scripts are rendered individually where they exist in the markup I have also created an httphandler, js.ashx, that does the actual combining of the scripts
Everything is working fine except for the "Head" location, for the two document locations i simply use the ClientScriptManager during prerender but for the Head location i have tried the following code during pre render
var scriptControl = new HtmlGenericControl("script");
scriptControl.Attributes["language"] = "javascript";
scriptControl.Attributes["type"] = "text/javascript";
scriptControl.Attributes["src"] = src;
Page.Header.Controls.Add(scriptControl);
and I get the following error: The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.
does anyone know how add a control to the page header from within a custom control?
Incidentally, the control is used on a content page that has two nested masters and also has a ScriptManager registered on the root master. The project is an asp.net 3.5 web application project