使用面板或占位符
ASP.NET 中的
和
有什么区别?
什么时候应该使用其中一种而不是另一种?
What is the difference between <asp:Panel >
and <asp:PlaceHolder >
in ASP.NET?
When should you use one over the other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
面板扩展为一个跨度(或一个 div),其中包含其内容。 占位符就是这样一个占位符,它会被您放入其中的任何内容替换。
A panel expands to a span (or a div), with it's content within it. A placeholder is just that, a placeholder that's replaced by whatever you put in it.
占位符本身不会呈现任何标签,因此它非常适合对内容进行分组,而无需外部 HTML 标签的开销。
该面板确实有外部 HTML 标签,但也有一些很酷的额外属性。
BackImageUrl:获取/设置
面板背景图像的 URL
HorizontalAlign:获取/设置
父级的水平对齐
内容
面板的内容换行
startvbnet 此处有一篇很好的文章。
The Placeholder does not render any tags for itself, so it is great for grouping content without the overhead of outer HTML tags.
The Panel does have outer HTML tags but does have some cool extra properties.
BackImageUrl: Gets/Sets the
background image's URL for the panel
HorizontalAlign: Gets/Sets the
horizontal alignment of the parent's
contents
panel's content wraps
There is a good article at startvbnet here.
PlaceHolder 控件
使用 PlaceHolder 控件作为用于存储动态添加到网页的服务器控件的容器。 PlaceHolder 控件不产生任何可见输出,仅用作网页上其他控件的容器。 您可以使用
Control.Controls
集合在 PlaceHolder 控件中添加、插入或删除控件。面板控件
面板控件是其他控件的容器。 当您想要以编程方式生成控件、隐藏/显示一组控件或本地化一组控件时,它特别有用。
Direction
属性对于本地化 Panel 控件的内容非常有用,以显示从右向左书写的语言(例如阿拉伯语或希伯来语)的文本。Panel 控件提供了多个属性,允许您自定义其内容的行为和显示。 使用
BackImageUr
l 属性显示 Panel 控件的自定义图像。 使用ScrollBars
属性指定控件的滚动条。呈现 HTML 时的细微差别:PlaceHolder 控件不会呈现任何内容,但 Panel 控件将呈现为
。
有关详细信息,请访问 ASP.NET 论坛
PlaceHolder control
Use the PlaceHolder control as a container to store server controls that are dynamically added to the Web page. The PlaceHolder control does not produce any visible output and is used only as a container for other controls on the Web page. You can use the
Control.Controls
collection to add, insert, or remove a control in the PlaceHolder control.Panel control
The Panel control is a container for other controls. It is especially useful when you want to generate controls programmatically, hide/show a group of controls, or localize a group of controls.
The
Direction
property is useful for localizing a Panel control's content to display text for languages that are written from right to left, such as Arabic or Hebrew.The Panel control provides several properties that allow you to customize the behavior and display of its contents. Use the
BackImageUr
l property to display a custom image for the Panel control. Use theScrollBars
property to specify scroll bars for the control.Small differences when rendering HTML: a PlaceHolder control will render nothing, but Panel control will render as a
<div>
.More information at ASP.NET Forums
我在 Visual Studio 2010 中发现了一个奇怪的错误*,如果您将控件放入占位符中,它不会在设计视图模式下呈现它们。
对于 Hidenfields 和 Empty 标签尤其如此。
我很想使用占位符而不是面板,但我讨厌这样一个事实:在 GUI 的设计时,我无法将其他控件放入占位符内。
I weird bug* in visual studio 2010, if you put controls inside a Placeholder it does not render them in design view mode.
This is especially true for Hidenfields and Empty labels.
I would love to use placeholders instead of panels but I hate the fact I cant put other controls inside placeholders at design time in the GUI.
正如其他答案中提到的,Panel 生成 HTML 中的
,而 PlaceHolder 则不会。 但您选择其中任何一个的原因还有很多。
为什么要使用 PlaceHolder?
由于它不生成自己的标签,因此您可以在不能包含
的其他元素中安全地使用它,例如:
您还可以使用一个占位符来控制一组控件的可见性,而不将其包装在
为什么是面板
它生成它自己的
但最有用的功能是
DefaultButton
属性。 当 ID 与面板中的按钮匹配时,当在文本框中按下enter
时,它将触发带有验证的表单发布。 现在,用户无需按按钮即可提交表单。通过在
TextBox1
内按enter
来尝试上面的代码片段As mentioned in other answers, the Panel generates a
<div>
in HTML, while the PlaceHolder does not. But there are a lot more reasons why you could choose either one.Why a PlaceHolder?
Since it generates no tag of it's own you can use it safely inside other element that cannot contain a
<div>
, for example:You can also use a PlaceHolder to control the Visibility of a group of Controls without wrapping it in a
<div>
Why a Panel
It generates it's own
<div>
and can also be used to wrap a group of Contols. But a Panel has a lot more properties that can be useful to format it's content:But the most useful feature is the
DefaultButton
property. When the ID matches a Button in the Panel it will trigger a Form Post with Validation whenenter
is pressed inside a TextBox. Now a user can submit the Form without pressing the Button.Try the above snippet by pressing
enter
insideTextBox1