I'm trying add some code at run time to the head element. When I do this in the page load of a user control in the page:
HtmlGenericControl hc = new HtmlGenericControl();
hc.InnerHtml = GetParameter("HeaderCode");
Page.Header.Controls.Add(hc);
The control ends up in the body tag, as the first child element. For Example:
...
</head>
<body>
<span><meta>Test</meta></span>
...
From my understanding, the Page.Header is supposed to be the Head element. In the aspx page, the head has the runat="server"
attribute set.
Any idea how I can inject a string in to the <head>
element?
The head element may have restrictions on the type of content. When we inject javascript into the header, we do it using a LiteralControl.
For example:
Page.Header.Controls.Add(New LiteralControl("<script language='javascript' type='text/javascript'>alert('test');</script>"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With