在 Render() 方法期间错误地写入 writer 时出现混乱
有人可以解释一下这里发生了什么吗?
我有以下标记:
<html>
<head runat="server">
<title>My title</title>
<my:MyControl runat="server" ID="myControl" />
</head>
...
我的自定义控件是这样的:
public MyControl : Control
{
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<script>this is my script</script>");
base.Render(writer);
}
}
渲染页面时,头部的整个上半部分被切掉,因此 html 渲染如下:
<html>
<script>this is my script</script>
</head>
解决方案是在 base.Render 之后调用 writer.Write ,像这样:
base.Render(writer);
writer.Write("<script>this is my script</script>");
为什么?!
更新
我对这个问题如此感兴趣感到惊讶!
我发现为什么我的 head 标签的顶部被删除了 - 这是我用来“清理”html 的 HttpHandler 的一个错误(它将脚本移动到底部等)。
这仍然不能准确解释为什么改变渲染方法的顺序会导致错误消失,但我确信这一切都有一个合乎逻辑的解释!
Could someone please explain what is going on here.
I have the following markup:
<html>
<head runat="server">
<title>My title</title>
<my:MyControl runat="server" ID="myControl" />
</head>
...
My custom control is something like this:
public MyControl : Control
{
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<script>this is my script</script>");
base.Render(writer);
}
}
When the page is rendered, the entire top half of the head is chopped off, so the html renders like this:
<html>
<script>this is my script</script>
</head>
The solution is to call writer.Write after base.Render, like this:
base.Render(writer);
writer.Write("<script>this is my script</script>");
Why?!
UPDATE
I'm surprised at the amount of interest in this question!
I found out why the top portion of my head tag was being removed - it was a bug with an HttpHandler that I use to 'clean' html (it moves scripts to the bottom etc).
This still doesn't explain exactly why changing the order of the render method would cause the bug to disappear, but I'm sure there is a logical explanation for it all!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只是用从函数返回的内容覆盖您的正文内容:
You simply are overwriting your body content with what are you returning from your function:
我会仔细查看这两种情况下呈现的 HTML。听起来您的脚本可能无法正确渲染,这将导致以下 html 无法渲染。更改顺序将更改 html 渲染的顺序,这意味着错误不会对页面产生太大影响。
I would look carefully at the rendered HTML in both case. It sounds like your script may be incorrectly rendering, which would cause the following html to fail to render. Changing the order will change the order of your html rendering, which means that the error is not impacting so much of the page.
以下是如何正确重写
Control.Render(HtmlTextWriter writer)
方法,向下滚动到示例部分:http://msdn.microsoft.com/en-us /library/system.web.ui.htmltextwriter.aspx
重要部分是
否则您可能会混淆
HtmlTextWriter
的缩进层次结构等。针对您的具体示例的更简单的解决方案 - 如果您不这样做不想以编程方式更改
标记的属性 - 是将标记
更改为仅
。
Here is how to correctly override the
Control.Render(HtmlTextWriter writer)
method, scroll down to the Examples section:http://msdn.microsoft.com/en-us/library/system.web.ui.htmltextwriter.aspx
The essential part is
Otherwise you may confuse the
HtmlTextWriter
's indentation hierarchy etc.A simpler solution to your concrete example - if you don't want to programmatically change the
<head>
tag's attributes - would be to change the tag<head runat="server">
to only<head>
.