HtmlGenericControl(“br”) rendering twice

前端 未结 2 1579
长情又很酷
长情又很酷 2020-12-20 17:08

I\'m adding some content to a given web page from code behind. When I want to add a break after some text, I try to do that this way:

pDoc.Controls.Add(New L         


        
2条回答
  •  粉色の甜心
    2020-12-20 17:46

    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:



    There is no
    tag in HTML, so the browser shows it as there are two
    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"); }
        }
    }
    

    And use it instead:

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

    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"))
    

提交回复
热议问题