Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

asp.net - HtmlGenericControl("br") rendering twice

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 Panel in which I'm adding the content. But it adds two br tags into the final 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,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

enter image description here

Also similar question here: Server control behaving oddly


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...