PlaceHolder控件应用示例
PlaceHolder控件可以为以编程方式动态添加的控件保留位置,从而将动态添加的服务器控件存储到Web页中。

PlaceHolderControl.aspx
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PlaceHolderControl.aspx.cs" Inherits="PlaceHolderControl" %>
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>PlaceHolder控件应用示例</title>
8
</head>
9
<body>
10
<form id="form1" runat="server">
11
<div>
12
PlaceHolder控件可以为以编程方式动态添加的控件保留位置,从而将动态添加的服务器控件存储到Web页中。<br />
13
<br />
14
<br />
15
<asp:PlaceHolder ID="phControl" runat="server"></asp:PlaceHolder>
16
17
</div>
18
</form>
19
</body>
20
</html>
21

PlaceHolderControl.aspx.cs
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 PlaceHolderControl : System.Web.UI.Page
13

{
14
protected void Page_Load(object sender, EventArgs e)
15
{
16
//在此处放置用户代码以初始化页面
17
HtmlButton myButton = new HtmlButton();
18
myButton.InnerText = "示例一";
19
//添加按钮到PlaceHolderControl控件
20
this.phControl.Controls.Add(myButton);
21
22
myButton = new HtmlButton();
23
myButton.InnerText = "示例二";
24
this.phControl.Controls.Add(myButton);
25
26
myButton = new HtmlButton();
27
myButton.InnerText = "示例三";
28
this.phControl.Controls.Add(myButton);
29
30
myButton = new HtmlButton();
31
myButton.InnerText = "示例四";
32
this.phControl.Controls.Add(myButton);
33
}
34
}
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PlaceHolderControl.aspx.cs" Inherits="PlaceHolderControl" %>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>PlaceHolder控件应用示例</title>8
</head>9
<body>10
<form id="form1" runat="server">11
<div>12
PlaceHolder控件可以为以编程方式动态添加的控件保留位置,从而将动态添加的服务器控件存储到Web页中。<br />13
<br />14
<br />15
<asp:PlaceHolder ID="phControl" runat="server"></asp:PlaceHolder>16
17
</div>18
</form>19
</body>20
</html>21

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 PlaceHolderControl : System.Web.UI.Page13


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

{16
//在此处放置用户代码以初始化页面17
HtmlButton myButton = new HtmlButton();18
myButton.InnerText = "示例一";19
//添加按钮到PlaceHolderControl控件20
this.phControl.Controls.Add(myButton);21

22
myButton = new HtmlButton();23
myButton.InnerText = "示例二"; 24
this.phControl.Controls.Add(myButton);25

26
myButton = new HtmlButton();27
myButton.InnerText = "示例三";28
this.phControl.Controls.Add(myButton);29

30
myButton = new HtmlButton();31
myButton.InnerText = "示例四";32
this.phControl.Controls.Add(myButton);33
}34
}
浙公网安备 33010602011771号