Insert a <br /> tag programmatically (VB.NET)

二次信任 提交于 2019-12-05 09:26:00

The proper control to use is the HtmlGenericControl.

Dim br As New HtmlGenericControl("br")

You can use the HtmlGenericControl to render any HTML element you wish, simply pass the element's tag name as a single argument to the constructor.

Why not just use another label, or append the <br> to the previous label.txt?

If the added
is the last element in the container div, you can not see any difference.

you can try :

Dim breakTag As LiteralControl
breakTag= New LiteralControl("<br />&nbsp;")
divListenerInfo.Controls.Add(breakTag)

to see the break.

But I think you should first add a dummy text into this Literal and search for it in your page if it's added. because your code looks fine.


I know this is reviving an old post but I didn't see any clear answer and I finally figured it out for my own application. Hope it helps someone else!
In my application I tried to create the control once and add it again wherever I needed it. The control would only be created once per parent.
You need to create a new control every time you need it!
So this:

divListenerInfo.Controls.Add(New LiteralControl("&lt;br />"))
divListenerInfo.Controls.Add(New LiteralControl("&lt;br />"))

Instead of this

Dim breakTag As LiteralControl
breakTag= New LiteralControl("&lt;br />")
divListenerInfo.Controls.Add(breakTag)
divListenerInfo.Controls.Add(breakTag)

I'm not sure if break is a reserve word also in vb.net so try

Dim newline = New LiteralControl("<br>")

or

newline.Text="<br>"; 

I would try stepping through the code with a debugger to make sure the line gets hit, also triple-check that divListenerInfo is the right control.

Nishad T. Kareem

I think you are using a panel or placeholder.

vb.net or C# .net syntax:

divListenerInfo.Controls.Add(New LiteralControl("< br >"))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!