PlaceHolder控件的功能与Panel控件的功能相似,PlaceHolder控件在某些情况下是非常有用的,比如需要在Panel控件中某一部分根据程序执行的过程动态地添加新的控件时就必须用到PlaceHolder控件。
aspx页面:
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
cs页面:
HtmlButton bt=new HtmlButton();//声明一个新的按钮
bt.InnerText="按钮添加";
PlaceHolder1.Controls.Add(bt);//添加到控件中
Literal htm = new Literal();//添加<br/>或<p>或普通text使用这种方式
htm.Text="<p></p>HTML代码<br/>";
PlaceHolder1.Controls.Add(htm);
以下是转自MSDN
http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.placeholder(VS.80).aspx
<%@ 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>