要通过编程向页面添加控件,则必须有放置新控件的容器。可以使用 PlaceHolder 或 Panel Web 服务器控件。示例:
1、创建控件的实例并设置其属性
private
UserControl demoControl;
demoControl = (UserControl)Page.LoadControl( " demos/TjSz.ascx " );
Label myLabel = new Label();
myLabel.Text = " Sample Label " ;
demoControl = (UserControl)Page.LoadControl( " demos/TjSz.ascx " );
Label myLabel = new Label();
myLabel.Text = " Sample Label " ;
2、将新控件添加到页上已有容器的 Controls 集合中
Panel Panel1
=
new
Panel();
Panel1.Controls.Add(myLabel);
PlaceHolder1.Controls.Add(demoControl);
Panel1.Controls.Add(myLabel);
PlaceHolder1.Controls.Add(demoControl);