在 Render() 方法期间错误地写入 writer 时出现混乱

发布于 2024-11-14 08:42:43 字数 986 浏览 1 评论 0原文

有人可以解释一下这里发生了什么吗?

我有以下标记:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月下客 2024-11-21 08:42:43

您只是用从函数返回的内容覆盖您的正文内容:

<script>this is my script</script>

You simply are overwriting your body content with what are you returning from your function:

<script>this is my script</script>
上课铃就是安魂曲 2024-11-21 08:42:43

我会仔细查看这两种情况下呈现的 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.

堇色安年 2024-11-21 08:42:43

以下是如何正确重写 Control.Render(HtmlTextWriter writer) 方法,向下滚动到示例部分:
http://msdn.microsoft.com/en-us /library/system.web.ui.htmltextwriter.aspx

重要部分是

        // Create a non-standard markup element.
        writer.RenderBeginTag("MyTag");
        writer.Write("Contents of MyTag");
        writer.RenderEndTag();
        writer.WriteLine();

否则您可能会混淆 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

        // Create a non-standard markup element.
        writer.RenderBeginTag("MyTag");
        writer.Write("Contents of MyTag");
        writer.RenderEndTag();
        writer.WriteLine();

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>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文