vb.net HtmlGenericControl("br") 渲染两次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13493586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HtmlGenericControl("br") rendering twice
提问by adripanico
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 Label With {.Text = "whatever"})
pDoc.Controls.Add(New HtmlGenericControl("br"))
,where pDoc is the Panelin which I'm adding the content. But it adds two brtags into the final HTML.
, 其中 pDoc 是Panel我添加内容的地方。但是它br在最终的 HTML 中添加了两个标签。
I've avoid this behaviour this way:
我已经通过这种方式避免了这种行为:
pDoc.Controls.Add(New Label With {.Text = "whatever" & "<br />"})
Anyway, I'm so curious and I want to know why
不管怎样,我很好奇,我想知道为什么
pDoc.Controls.Add(New HtmlGenericControl("br"))
is acting that way. I also think my approach is not too fancy.
正在那样做。我也觉得我的做法不太花哨。
Regards,
问候,
采纳答案by tpeczek
After some testing it looks like the reason is that HtmlGenericControldoesn't support self closing. On server side the HtmlGenericControl("br")is treated as:
经过一些测试,看起来原因是HtmlGenericControl不支持self closed。在服务器端HtmlGenericControl("br")被视为:
<br runat="server"></br>
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 HtmlGenericSelfCloseControllike this (sorry for C# code but you should have no issue with rewritting this in VB.NET):
</br>HTML 中没有标签,因此浏览器将其显示为有两个<br />标签。解决这个问题的好方法是HtmlGenericSelfCloseControl像这样创建(对不起 C# 代码,但在 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:
作为更简单的替代方案(如果您参考了Page),您可以尝试使用Page.ParseControl:
pDoc.Controls.Add(New Label With {.Text = "whatever"})
pDoc.Controls.Add(Page.ParseControl("br"))
回答by Berkay Turanc?
Actually you can use;
其实你可以使用;
pDoc.Controls.Add(new LiteralControl("<br/>"));
Whereas new HtmlGenericControl("br")adds two <br>, this will only add <br/>tag to your HTML so that you just have 1 space line.
In this picture I added those breaks with that code block.
而new HtmlGenericControl("br")添加两个<br>,这只会<br/>向您的 HTML添加标签,因此您只有 1 个空格行。在这张图片中,我使用该代码块添加了这些中断。


Also similar question here: Server control behaving oddly
这里也有类似的问题:服务器控制行为奇怪

