无法获得头内的内容,因为内容不是文字。 - 尝试追加到页眉页眉、内容、文字、不是

2023-09-07 09:06:52 作者:何以畏孤独

我有一个aspx页面头部

 <头=服务器ID =头>
    <冠军>< /标题>
< /头>
 

现在的网站我的工作是从一个XML文件中提取信息,我想采取一个节点从XML文件,并把它添加到头部。

 <头=服务器ID =头>
        <冠军>< /标题>
        <脚本>< / SCRIPT> //来自XML文件
    < /头>
 

然而,当我尝试任何其中之一。

 保护无效Page_Init(对象发件人,EventArgs的)
    {


        this.Page.Header.InnerHtml + = xml.Header; //错误出
        this.Page.Header.InnerHtml = this.Page.Header.InnerHtml + xml.Header; // errot UT

    }
 
编辑页眉时,页眉的文字与下横线离得太远,如何时调整

它的错误了

  

System.Web.HttpException是未处理由用户code消息=无法   让头部内的内容,因为内容不是文字。

如果我这样做

 保护无效Page_Init(对象发件人,EventArgs的)
        {


            this.Page.Header.InnerHtml = xml.Header;


        }
 

一切都很好。

如果我这样做

 <头=服务器ID =头>
   //去掉标题标签所以现在没有什么是头
< /头>
 

,然后做到这一点

 保护无效Page_Init(对象发件人,EventArgs的)
        {


            this.Page.Header.InnerHtml + = xml.Header; // 作品
            this.Page.Header.InnerHtml = this.Page.Header.InnerHtml + xml.Header; //作品

        }
 

它的工作原理。所以,我不知道失败的原因时,有头已经内标签。

如果我也尝试着从头部得到的内容,以及我得到同样的错误

  VAR一个= this.Page.Header.InnerHtml;
VAR一个= this.Page.Header.InnerText;
 

解决方案

此问题可能是由于头标记是越来越与它的脚本标记呈现方式

它也可以是该页面是越来越呈现太快

我会建议您尝试插入头标记的脚本标签,而页面越来越PAGE_ preRender事件

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