开发者

ASP.NET dynamically insert code into head

I'm working inside of a Web User Control (.ascx) that is going to be included in a regular web form (.aspx), but I need to be able to dynamically insert code into the head of the document from the User Control. In my Coldfusion days <cfhtmlhead> would do the trick. Is there an equivalent 开发者_开发问答of this in ASP.NET or a similar hack?


To add HTML markup you can do the following:

In your UserControl's code you can access Page.Header, which is itself a control. To that control you can then add new controls:

HtmlGenericControl newControl = new HtmlGenericControl("someTag");
newControl.Attributes["someAttr"] = "some value";
Page.Header.Controls.Add(newControl);

To add script markup you don't need access to the head tag at all since ASP.NET has helper methods on the ClientScriptManager that do the work for you:

Here are examples of some code you can also put in your user control's code:

// Register some inline script:
Page.ClientScript.RegisterClientScriptBlock(GetType(), "myAlertScript", "alert('hello!')", true);

// Register a script reference:
Page.ClientScript.RegisterClientScriptInclude(GetType(), "myLibraryScript", "~/Scripts/MyScriptLibrary.js");


I realize that this is an old question, but this is another example.

Try This:

Page.Header.Controls.Add(
    new LiteralControl(
        "<script>alert('Literal Added to <Head>.');</script>"
    )
);

If you want to add the script at a particular index of the <head> you can use

AddAt(index, new LiteralControl(...)) where index 0 equals the top of the <head>

Also, you need to add runat="server" in your head tag e.g. <head id="head1" runat="server">


this.Page.Header.Controls.Add

By doing this, you are adding controls to the head section. You can add any type of control. If you feel you need to add simple text (or you want to write the tags manually), then look into the LiteralControl class.


There's some guidance on using C# code to modify the page header here. It should work just fine from any server-side code that executes before the page load completes.

A simple e.g.

HtmlHead head = Page.Header;
HtmlTitle title = new HtmlTitle();
title.Text = "Test Page";
head.Controls.Add(title);

HTMLHead reference is in namespace

System.Web.UI.HtmlControls

Override the custom control's Load() method to add the controls or references you need into the page header while the parent .aspx page is being loaded server-side.


I have a simple solution for this. Create a runtime memory cache based on the url of the page (as a key) that holds x information about y (be it a file reference, script text, or class that generates JavaScript) and serialize its data to JSON. Newtonsoft is helpful for instances of any class. In fact, you can use it's output to initialize any new instance of a class based upon given input. In a way, that means you may have your instances of any particular class automatically instantiated despite what user control the instance is on. In the end, you create a simple web form to serve as a script reference and as the final endpoint. It pulls the JavaScript (or what've it) and spits out the client side code you need as a script reference inside the head tag.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜