以编程方式插入 <br /> 标签 (VB.NET)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/799226/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:36:01  来源:igfitidea点击:

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

.netvb.netliterals

提问by Paxenos

I'm trying to dynamically add results to this display and I simply want to put a break tag after a label to start putting information on the next line. For some reason, Using a literal isn't working for me. Is there a better way to do this or should I just use tables?

我正在尝试将结果动态添加到此显示中,我只想在标签后放置一个中断标记以开始在下一行放置信息。出于某种原因,使用文字对我不起作用。有没有更好的方法来做到这一点,还是我应该只使用表格?

Dim break As LiteralControl
break = New LiteralControl("<br />")
divListenerInfo.Controls.Add(break)

That's part of the code that I'm attempting to use.

这是我尝试使用的代码的一部分。



让我澄清一下我所说的:

It's not working as in the line break isn't showing up on the webpage. It's compiling fine and there is nothing wrong with the code. It just doesn't show up in the html for some odd reason.

它不起作用,因为换行符没有显示在网页上。它编译得很好,代码没有任何问题。它只是出于某种奇怪的原因没有出现在 html 中。

回答by Andrew Hare

The proper control to use is the HtmlGenericControl.

要使用的正确控件是HtmlGenericControl.

Dim br As New HtmlGenericControl("br")

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

您可以使用HtmlGenericControl来呈现您希望的任何 HTML 元素,只需将元素的标签名称作为单个参数传递给构造函数。

回答by madcolor

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

为什么不直接使用另一个标签,或者将 附加<br>到之前的 label.txt 中?

回答by TemiGiwa


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)

回答by Canavar

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

如果添加的
是容器 div 中的最后一个元素,则看不出任何区别。

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.

但我认为你应该首先在这个 Literal 中添加一个虚拟文本,如果它被添加,则在你的页面中搜索它。因为你的代码看起来不错。

回答by TStamper

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

我不确定 break 是否也是 vb.net 中的保留字,所以试试

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

or

或者

newline.Text="<br>"; 

回答by Alex

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

我会尝试使用调试器单步执行代码以确保该行被命中,同时三重检查divListenerInfo是否正确。

回答by Nishad T. Kareem

I think you are using a panel or placeholder.

我认为您正在使用面板或占位符。

vb.net or C# .net syntax:

vb.net 或 C# .net 语法:

divListenerInfo.Controls.Add(New LiteralControl("< br >"))