使用面板或占位符

发布于 2024-07-12 06:10:57 字数 142 浏览 10 评论 0原文

ASP.NET 中的 有什么区别?

什么时候应该使用其中一种而不是另一种?

What is the difference between <asp:Panel > and <asp:PlaceHolder > in ASP.NET?

When should you use one over the other?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

盗梦空间 2024-07-19 06:10:57

面板扩展为一个跨度(或一个 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.

始终不够爱げ你 2024-07-19 06:10:57

占位符本身不会呈现任何标签,因此它非常适合对内容进行分组,而无需外部 HTML 标签的开销。

该面板确实有外部 HTML 标签,但也有一些很酷的额外属性。

  • BackImageUrl:获取/设置
    面板背景图像的 URL

  • Horizo​​ntalAlign:获取/设置
    父级的水平对齐
    内容

  • 换行:获取/设置是否
    面板的内容换行

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

  • Wrap: Gets/Sets whether the
    panel's content wraps

There is a good article at startvbnet here.

迷荒 2024-07-19 06:10:57

PlaceHolder 控件

使用 PlaceHolder 控件作为用于存储动态添加到网页的服务器控件的容器。 PlaceHolder 控件不产生任何可见输出,仅用作网页上其他控件的容器。 您可以使用 Control.Controls 集合在 PlaceHolder 控件中添加、插入或删除控件。

面板控件

面板控件是其他控件的容器。 当您想要以编程方式生成控件、隐藏/显示一组控件或本地化一组控件时,它特别有用。

Direction 属性对于本地化 Panel 控件的内容非常有用,以显示从右向左书写的语言(例如阿拉伯语或希伯来语)的文本。

Panel 控件提供了多个属性,允许您自定义其内容的行为和显示。 使用 BackImageUrl 属性显示 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 BackImageUrl property to display a custom image for the Panel control. Use the ScrollBars 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

哽咽笑 2024-07-19 06:10:57

我在 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.

倾其所爱 2024-07-19 06:10:57

正如其他答案中提到的,Panel 生成 HTML 中的

,而 PlaceHolder 则不会。 但您选择其中任何一个的原因还有很多。

为什么要使用 PlaceHolder?

由于它不生成自己的标签,因此您可以在不能包含

的其他元素中安全地使用它,例如:

<table>
    <tr>
        <td>Row 1</td>
    </tr>
    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</table>

您还可以使用一个占位符来控制一组控件的可见性,而不将其包装在

<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible="false">
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <br />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</asp:PlaceHolder>

为什么是面板

它生成它自己的

也可用于包装一组 Contols。 但面板还有更多属性可用于格式化其内容:

<asp:Panel ID="Panel1" runat="server" Font-Bold="true"
    BackColor="Green" ForeColor="Red" Width="200"
    Height="200" BorderColor="Black" BorderStyle="Dotted">
    Red text on a green background with a black dotted border.
</asp:Panel>

但最有用的功能是 DefaultButton 属性。 当 ID 与面板中的按钮匹配时,当在文本框中按下 enter 时,它将触发带有验证的表单发布。 现在,用户无需按按钮即可提交表单。

<asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
        ErrorMessage="Input is required" ValidationGroup="myValGroup"
        Display="Dynamic" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="myValGroup" />
</asp:Panel>

通过在 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:

<table>
    <tr>
        <td>Row 1</td>
    </tr>
    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</table>

You can also use a PlaceHolder to control the Visibility of a group of Controls without wrapping it in a <div>

<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible="false">
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <br />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</asp:PlaceHolder>

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:

<asp:Panel ID="Panel1" runat="server" Font-Bold="true"
    BackColor="Green" ForeColor="Red" Width="200"
    Height="200" BorderColor="Black" BorderStyle="Dotted">
    Red text on a green background with a black dotted border.
</asp:Panel>

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 when enter is pressed inside a TextBox. Now a user can submit the Form without pressing the Button.

<asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
        ErrorMessage="Input is required" ValidationGroup="myValGroup"
        Display="Dynamic" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="myValGroup" />
</asp:Panel>

Try the above snippet by pressing enter inside TextBox1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文