PlaceHolder vs Literal用于添加在运行时生成的HTML标记

时间:2021-08-06 09:47:02

This question points out Literal vs Label while this question points out Panel VS. PlaceHolder but just today I was debating with my colleague on using PlacHolder vs Literal for adding HTML markup which is generated at runtime. Both controls do not produce any extra markup but we are looking for the right control for adding generated markup on the fly. The answer of this question suggests using of both for adding generated markup so I'm wondering which control/approach should we use for just adding generated markup and nothing more.

这个问题指出Literal vs Label,而这个问题指出了Panel VS. PlaceHolder但就在今天我和我的同事一起讨论使用PlacHolder vs Literal来添加在运行时生成的HTML标记。两个控件都不会产生任何额外的标记,但我们正在寻找正确的控件来动态添加生成的标记。这个问题的答案建议使用两者来添加生成的标记,所以我想知道我们应该使用哪种控件/方法来添加生成的标记,仅此而已。

2 个解决方案

#1


14  

Neither render any markup of their own (which can be a very good thing). However, a Placeholder may contain child controls, whereas a Literal cannot.

既不渲染任何自己的标记(这可能是一件非常好的事情)。但是,占位符可能包含子控件,而Literal则不能。

By comparison, a Placeholder can contain other controls, but does not have a Text property.

相比之下,占位符可以包含其他控件,但没有Text属性。

I'm wondering which control/approach should we use for just adding generated markup and nothing more.

我想知道我们应该使用哪种控制/方法来添加生成的标记,仅此而已。

If by "generated" you mean the end result is a string, I would use a Literal. If you are generating a control tree, then append those controls to a Placeholder.

如果通过“生成”表示最终结果是字符串,我会使用文字。如果要生成控制树,请将这些控件附加到占位符。

Or, if you want to omit the declaration of a server control completely:

或者,如果要完全省略服务器控件的声明:

<h2>Hello World</h2>
<p>The following is generated markup.</p>
<%= base.GetGeneratedMarkup() %>

I believe a Literal is still generated under the hood for this, but it allows you to mix generated content with the markup portion of your page/control (similar to Razor).

我相信Literal仍然是为此而生成的,但它允许您将生成的内容与页面/控件的标记部分混合(类似于Razor)。

#2


6  

PlaceHolder vs Literal for adding HTML

The main different is the the Literal control have the Text property (and the Mode) that is also saved on the Viewstate, the PlaceHolder is totally empty.

主要的不同是Literal控件具有Text属性(和Mode)也保存在Viewstate上,PlaceHolder完全为空。

So you can use a Literal control to add direct some string html code, and you can use the PlaceHolder to add some other controls but not adding any direct html code.

因此,你可以使用Literal控件直接添加一些字符串html代码,你可以使用PlaceHolder添加一些其他控件但不添加任何直接的HTML代码。

#1


14  

Neither render any markup of their own (which can be a very good thing). However, a Placeholder may contain child controls, whereas a Literal cannot.

既不渲染任何自己的标记(这可能是一件非常好的事情)。但是,占位符可能包含子控件,而Literal则不能。

By comparison, a Placeholder can contain other controls, but does not have a Text property.

相比之下,占位符可以包含其他控件,但没有Text属性。

I'm wondering which control/approach should we use for just adding generated markup and nothing more.

我想知道我们应该使用哪种控制/方法来添加生成的标记,仅此而已。

If by "generated" you mean the end result is a string, I would use a Literal. If you are generating a control tree, then append those controls to a Placeholder.

如果通过“生成”表示最终结果是字符串,我会使用文字。如果要生成控制树,请将这些控件附加到占位符。

Or, if you want to omit the declaration of a server control completely:

或者,如果要完全省略服务器控件的声明:

<h2>Hello World</h2>
<p>The following is generated markup.</p>
<%= base.GetGeneratedMarkup() %>

I believe a Literal is still generated under the hood for this, but it allows you to mix generated content with the markup portion of your page/control (similar to Razor).

我相信Literal仍然是为此而生成的,但它允许您将生成的内容与页面/控件的标记部分混合(类似于Razor)。

#2


6  

PlaceHolder vs Literal for adding HTML

The main different is the the Literal control have the Text property (and the Mode) that is also saved on the Viewstate, the PlaceHolder is totally empty.

主要的不同是Literal控件具有Text属性(和Mode)也保存在Viewstate上,PlaceHolder完全为空。

So you can use a Literal control to add direct some string html code, and you can use the PlaceHolder to add some other controls but not adding any direct html code.

因此,你可以使用Literal控件直接添加一些字符串html代码,你可以使用PlaceHolder添加一些其他控件但不添加任何直接的HTML代码。