I have an aspx page with a head
<head runat="server" id="head">
<title></title>
</head>
Now the site I am working on is pulling information from a xml file and I want to take a node from the xml file and append it to the head section.
<head runat="server" id="head">
<title></title>
<script></script> // comes from the xml file
</head>
However when I try to either one of these.
protected void Page_Init(object sender, EventArgs e)
{
this.Page.Header.InnerHtml += xml.Header; // errors out
this.Page.Header.InnerHtml = this.Page.Header.InnerHtml + xml.Header; // errot ut
}
It errors out
System.Web.HttpException was unhandled by user code Message=Cannot
get inner content of head because the contents are not literal.
If I do this
protected void Page_Init(object sender, EventArgs e)
{
this.Page.Header.InnerHtml = xml.Header;
}
all is fine.
If I do this
<head runat="server" id="head">
// removed the title tag so now nothing is in head
</head>
and then do this
protected void Page_Init(object sender, EventArgs e)
{
this.Page.Header.InnerHtml += xml.Header; // works
this.Page.Header.InnerHtml = this.Page.Header.InnerHtml + xml.Header; //works
}
It works. So I am not sure why it fails when there is tags inside the head already.
If I also try to get the contents from the head as well I get the same error
var a = this.Page.Header.InnerHtml;
var a = this.Page.Header.InnerText;
This issue might be due to the way the head tag is getting rendered with the script tag in it
It can also be that the page is getting render too quickly
I would suggest that you try insert the script tag in the head tag while page is getting Page_PreRender event