无法获得头内的内容,因为内容不是文字。 - 尝试追加到页眉 [英] Cannot get inner content of head because the contents are not literal. - Trying to append to Header

查看:186
本文介绍了无法获得头内的内容,因为内容不是文字。 - 尝试追加到页眉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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

这篇关于无法获得头内的内容,因为内容不是文字。 - 尝试追加到页眉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆