且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

HtmlGenericControl(QUOT; BR&Q​​UOT;)渲染两次

更新时间:2023-02-17 13:03:12

一些测试它看起来像的原因之后是 HtmlGenericControl 不支持的自动关闭的。在服务器端的 HtmlGenericControl(BR)被视为:

After some testing it looks like the reason is that HtmlGenericControl doesn't support self closing. On server side the HtmlGenericControl("br") is treated as:

<br runat="server"></br>

有没有&LT; / BR&gt;在HTML 标签,所以浏览器显示它作为有两个&LT; BR /&GT; 标记。尼斯出路是创建 HtmlGenericSelfCloseControl 这样的(对不起,C#code,但你应该没有问题,在VB.NET rewritting本):

There is no </br> tag in HTML, so the browser shows it as there are two <br /> tags. Nice way out of this is to create HtmlGenericSelfCloseControl like this (sorry for C# code but you should have no issue with rewritting this in VB.NET):

public class HtmlGenericSelfCloseControl : HtmlGenericControl
{
    public HtmlGenericSelfCloseControl()
        : base()
    {
    }

    public HtmlGenericSelfCloseControl(string tag)
        : base(tag)
    {
    }

    protected override void Render(HtmlTextWriter writer)
    {
        writer.Write(HtmlTextWriter.TagLeftChar + this.TagName);
        Attributes.Render(writer);
        writer.Write(HtmlTextWriter.SelfClosingTagEnd);
    }

    public override ControlCollection Controls
    {
        get { throw new Exception("Self closing tag can't have child controls"); }
    }

    public override string InnerHtml
    {
        get { return String.Empty; }
        set { throw new Exception("Self closing tag can't have inner content"); }
    }

    public override string InnerText
    {
        get { return String.Empty; }
        set { throw new Exception("Self closing tag can't have inner text"); }
    }
}

和使用它来代替:

pDoc.Controls.Add(New Label With {.Text = "whatever"})
pDoc.Controls.Add(New HtmlGenericSelfCloseControl("br"))

作为一个更简单的选择(如果你有参考),你可以尝试使用 Page.ParseControl

As a simpler alternative (if you have reference to the Page) you can try using Page.ParseControl:

pDoc.Controls.Add(New Label With {.Text = "whatever"})
pDoc.Controls.Add(Page.ParseControl("br"))