閱讀此文請先查看網頁教學網的:ASP.NET入門教程:Web服務器控件,簡單講述了Web服務器控件的使用方法。
PlaceHolder 控件用於為代碼添加的控件預留空間。在頁控件層次結構中為以編程方式添加的控件保留位置。
注釋:PlaceHolder 不會產生任何可見的輸出(僅僅是網頁上其它控件的容器)。
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>