C# 无法获取标头的内部内容,因为内容不是文字。-正在尝试附加到标头
我有一个带标题的aspx页面C# 无法获取标头的内部内容,因为内容不是文字。-正在尝试附加到标头,c#,asp.net,.net,header,C#,Asp.net,.net,Header,我有一个带标题的aspx页面 <head runat="server" id="head"> <title></title> </head> 它出错了 System.Web.HttpException未被用户代码消息处理=无法 获取头部的内部内容,因为内容不是文字 如果我这样做 protected void Page_Init(object sender, EventArgs e) { this
<head runat="server" id="head">
<title></title>
</head>
它出错了
System.Web.HttpException未被用户代码消息处理=无法
获取头部的内部内容,因为内容不是文字
如果我这样做
protected void Page_Init(object sender, EventArgs e)
{
this.Page.Header.InnerHtml = xml.Header;
}
<head runat="server" id="head">
// removed the title tag so now nothing is in head
</head>
一切都很好
如果我这样做
protected void Page_Init(object sender, EventArgs e)
{
this.Page.Header.InnerHtml = xml.Header;
}
<head runat="server" id="head">
// removed the title tag so now nothing is in head
</head>
它起作用了。因此,我不知道为什么它失败时,有标签内的头部已经
如果我也尝试从头部获取内容,我会得到相同的错误
var a = this.Page.Header.InnerHtml;
var a = this.Page.Header.InnerText;
此问题可能是由于head标记与其中的script标记一起呈现的方式造成的 也可能是页面渲染过快
我建议您在page获取page_PreRender事件时尝试在head标记中插入script标记您可以使用(VB)完成此操作: 现在您可以使用变量“s”对其执行任何操作
不要忘记导入System.IO以使其正常工作。问题在于使用母版页时。this.Page.Header.InnerHtml无效您希望从head标记中获取哪些内容。。你有没有试过使用脚本标签来获取你需要的信息?我不确定你想从标签中获取什么。我想把一切都记在脑子里。无论是在它的标题,链接,内容的地方持有人,脚本。所以我并不想通过它来解析它,它应该是一个转储文件。如果你只是想把它附加到头文件中,那么你就错了。当我认为您应该像处理XML一样处理它时,您就像连接字符串一样连接它!这就是我的意思。。脚本只是一个例子。xml文件中可以包含任何内容(对于有效的头,任何内容都可以)。它可以有脚本,链接等等。我可以尝试Page_PreRender,看看会发生什么。
var a = this.Page.Header.InnerHtml;
var a = this.Page.Header.InnerText;
Dim sw As New StringWriter()
Dim w As New HtmlTextWriter(sw)
NameOfYourElementID.RenderControl(w)
Dim s As String = sw.GetStringBuilder().ToString()