前台:
1

<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="panel_PlaceHolder.aspx.cs" Inherits="panel_PlaceHolder" %>2

3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">4

5
<html xmlns="http://www.w3.org/1999/xhtml" >6
<head runat="server">7
<title>无标题页</title>8
</head>9
<body>10
<form id="form1" runat="server">11
<div>12
================== Panel ==========<br />13
<asp:Panel ID="Panel1" runat="server" BackColor="#FFE0C0" Height="50px" Width="125px">14
</asp:Panel>15
16
<br />17
添加<asp:DropDownList ID="DropDownList1" runat="server" Width="96px">18
</asp:DropDownList>个到文本标签<br />19
添加<asp:DropDownList ID="DropDownList2" runat="server" Width="95px">20
</asp:DropDownList>个到文本框<br />21
添加<asp:TextBox ID="txt" runat="server" Width="88px"></asp:TextBox>进去(文字)<br />22
<asp:Button ID="AddToPanel" runat="server" OnClick="AddToPanel_Click" Text="添加进去" />23
24
<asp:CheckBox ID="YcPanel" runat="server" AutoPostBack="True" OnCheckedChanged="YcPanel_CheckedChanged"25
Text="隐藏 Panel" /><br />26
=================================<br />27
<br />28
=================== PlaceHolder =====<br />29
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>30
<br />31
<br />32
<asp:Button ID="Button1" runat="server" Text="AddToPlaceHolder" OnClick="Button1_Click" /><br />33
<br />34
=================================<br />35
36
</div>37
</form>38
</body>39
</html>40

后台:
1
using System;2
using System.Data;3
using System.Configuration;4
using System.Collections;5
using System.Web;6
using System.Web.Security;7
using System.Web.UI;8
using System.Web.UI.WebControls;9
using System.Web.UI.WebControls.WebParts;10
using System.Web.UI.HtmlControls;11

12
public partial class panel_PlaceHolder : System.Web.UI.Page13


{14
protected void Page_Load(object sender, EventArgs e)15

{16
if (!Page.IsPostBack)17

{18
for (int i = 1; i <= 3;i++ )19

{20
this.DropDownList1.Items.Add(i.ToString());21
this.DropDownList2.Items.Add(i.ToString());22
}23
}24
}25
protected void AddToPanel_Click(object sender, EventArgs e)26

{27
//注意 a 的取值方法.28
int a = int.Parse(this.DropDownList1.SelectedItem.Value);29
for (int i = 1; i <= a; i++)30

{31
Label lb = new Label();32
lb.ID = "lb" + i.ToString();33
lb.Text = "高峰" + i.ToString()+"<br>";34
Panel1.Controls.Add(lb);35
}36

37
//注意 b 的取值.38
int b = int.Parse(this.DropDownList2.SelectedItem.Value);39
for (int i = 1; i <= b;i++ )40

{41
TextBox tb = new TextBox();42
tb.ID = "tb" + i.ToString();43
tb.Text = "高峰" + i.ToString();44
Literal lt = new Literal();45
lt.Text = "<br>";46
//添加到 Panel 中47
Panel1.Controls.Add(tb);48
Panel1.Controls.Add(lt);49
}50
Literal ltr = new Literal();51
ltr.Text = this.txt.Text;52
Panel1.Controls.Add(ltr);53
}54
protected void YcPanel_CheckedChanged(object sender, EventArgs e)55

{56
if (this.YcPanel.Checked)57

{58
this.Panel1.Visible = false;59
this.YcPanel.Text = "显示 Panel";60
}61
else62

{63
this.Panel1.Visible = true;64
this.YcPanel.Text = "隐藏 Panel";65
}66
}67
protected void Button1_Click(object sender, EventArgs e)68

{69
HtmlButton hbt = new HtmlButton();70
hbt.InnerText = "我是一个 Html 按钮!";71

72
Literal lt = new Literal();73
lt.Text = "<br>";74

75
Button bt = new Button();76
bt.Text = "我是一个 服务器 按钮";77

78
//然后将这三个控件添加到 PlaceHolder 中79
this.PlaceHolder1.Controls.Add(hbt);80
this.PlaceHolder1.Controls.Add(lt);81
this.PlaceHolder1.Controls.Add(bt);82

83
84
}85
}86
