Web服务器控件:PlaceHolder控件
阅读此文请先查看IT教学网的:ASP.NET入门教程:Web服务器控件,简单讲述了Web服务器控件的使用方法。
定义和用法
PlaceHolder 控件用于为代码添加的控件预留空间。在页控件层次结构中为以编程方式添加的控件保留位置。
注释:PlaceHolder 不会产生任何可见的输出(仅仅是网页上其它控件的容器)。
属性
属性 | 描述 | .NET |
---|---|---|
runat | 规定控件是服务器。必须设置为 "server"。 | 1.0 |
Web 控件标准属性
AccessKey, Attributes, BackColor, BorderColor, BorderStyle, BorderWidth, CssClass, Enabled, Font, EnableTheming, ForeColor, Height, IsEnabled, SkinID, Style, TabIndex, ToolTip, Width
控件标准属性
AppRelativeTemplateSourceDirectory, BindingContainer, ClientID, Controls, EnableTheming, EnableViewState, ID, NamingContainer, Page, Parent, Site, TemplateControl, TemplateSourceDirectory, UniqueID, Visible
语法:
<asp:PlaceHolder
EnableTheming="True|False"
EnableViewState="True|False"
ID="string"
OnDataBinding="DataBinding event handler"
OnDisposed="Disposed event handler"
OnInit="Init event handler"
OnLoad="Load event handler"
OnPreRender="PreRender event handler"
OnUnload="Unload event handler"
runat="server"
SkinID="string"
Visible="True|False"
/>
备注:将 PlaceHolder 控件用作容器,从而将动态添加的服务器控件存储到网页中。PlaceHolder 控件不产生任何可见的输出,仅用作网页上其他控件的容器。您可以使用 Control.Controls 集合在 PlaceHolder 控件中添加、插入或移除控件。
实例:
下面的示例演示如何将 Web 服务器控件添加到 PlaceHolder 控件中。
Visual Basic
<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
<script runat="server">
Sub Page_Load(Sender As Object, e As EventArgs)
Dim myButton As HtmlButton = New HtmlButton()
myButton.InnerText = "Button 1"
PlaceHolder1.Controls.Add(myButton)
myButton = New HtmlButton()
myButton.InnerText = "Button 2"
PlaceHolder1.Controls.Add(myButton)
myButton = New HtmlButton()
myButton.InnerText = "Button 3"
PlaceHolder1.Controls.Add(myButton)
myButton = New HtmlButton()
myButton.InnerText = "Button 4"
PlaceHolder1.Controls.Add(myButton)
End Sub
</script>
</head>
<body>
<form runat="server">
<h3>PlaceHolder Example</h3>
<asp:PlaceHolder id="PlaceHolder1"
runat="server"/>
</form>
</body>
</html>
C#
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
HtmlButton myButton = new HtmlButton();
myButton.InnerText = "Button 1";
PlaceHolder1.Controls.Add(myButton);
myButton = new HtmlButton();
myButton.InnerText = "Button 2";
PlaceHolder1.Controls.Add(myButton);
myButton = new HtmlButton();
myButton.InnerText = "Button 3";
PlaceHolder1.Controls.Add(myButton);
myButton = new HtmlButton();
myButton.InnerText = "Button 4";
PlaceHolder1.Controls.Add(myButton);
}
</script>
</head>
<body>
<form runat="server">
<h3>PlaceHolder Example</h3>
<asp:PlaceHolder id="PlaceHolder1"
runat="server"/>
</form>
</body>
</html>