问题描述:

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

网友答案:

Ive discovered an answer to my question.

I don't quite understand the why but the problem lies in when I am trying to add my script control into the head, doing it in the control's PreRender event causes my error but if you add the control during the Page's PreRender event it all works fine and dandy eg:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    this.Page.PreRender += new EventHandler(Page_PreRender);
}

void Page_PreRender(object sender, EventArgs e)
{
    var scriptControl = new HtmlGenericControl("script");
    Page.Header.Controls.Add(scriptControl);
    scriptControl.Attributes["language"] = "javascript";
    scriptControl.Attributes["type"] = "text/javascript";
    scriptControl.Attributes["src"] = "blah.js";
}
网友答案:

I don't know why you get this error but how about using ClientScript like that :

protected void Page_Load(object sender, EventArgs e)
{
    string scriptFile = "myscriptFile.js";

    if (!this.Page.ClientScript.IsClientScriptIncludeRegistered("myScript"))
    {
        this.Page.ClientScript.RegisterClientScriptInclude("myScript", scriptFile);
    }
}

ClientScriptManager.RegisterClientScriptInclude Method

网友答案:
protected override void OnInit(EventArgs e)
{
    EnsureChildControls();
    base.OnInit(e);
}
网友答案:
  protected void Page_Load(object sender, EventArgs e)
    {        
        HtmlGenericControl js = new HtmlGenericControl("script");
        js.Attributes["type"] = "text/javascript";
        js.Attributes["src"] = yol.ScriptYol("jquery-1.3.2.min.js");
        this.Page.Header.Controls.Add(js);

    }
相关阅读:
Top