Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a control to the page header in ASP.NET

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

like image 953
Lightweight Avatar asked May 20 '09 21:05

Lightweight


People also ask

How to add user control in Html page?

To include a user control in a Web Forms pageA TagPrefix attribute, which associates a prefix with the user control. This prefix will be included in opening tag of the user control element. A TagName attribute, which associates a name with the user control.

How do I add a control to a Web form?

If we want to add some control on web page, we can use Toolbox on the left side in Visual Studio. All the server side controls are placed in Toolbox panel. Open Toolbox and just select control drag and drop control to web form where you want.

What is ASP.NET header?

Request headers are a great feature in ASP.NET Core that enable you to work with optional data represented as a collection of key-value pairs that can be transmitted back and forth between the client and server. The Request class provides access to metadata as well as headers of the HttpContext.


1 Answers

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";
}
like image 176
Lightweight Avatar answered Sep 28 '22 19:09

Lightweight