1、例程
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
for (int i = 1; i < 4; i++)
{
Label lb_1 = new Label();
lb_1.Text = "(^_^)加载第" + i.ToString() + "个Label控件";
lb_1.ID = i.ToString();
Panel1.Controls.Add(lb_1);
Panel1.Controls.Add(new LiteralControl("<br/>"));
}
}
}
2、结合ispostback属性加深理解
protected void Page_Load(object sender, EventArgs e)
{
{
for (int i = 1; i < 4; i++)
{
Label lb_1 = new Label();
lb_1.Text = "(^_^)加载第" + i.ToString() + "个Label控件";
lb_1.ID = i.ToString();
Panel1.Controls.Add(lb_1);
Panel1.Controls.Add(new LiteralControl("<br/>"));
}
}
}
这种情况下,首次显示,单击按钮将web页面postback回服务器,三个label控件都会存在,显示
3、
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
for (int i = 1; i < 4; i++)
{
Label lb_1 = new Label();
lb_1.Text = "(^_^)加载第" + i.ToString() + "个Label控件";
lb_1.ID = i.ToString();
Panel1.Controls.Add(lb_1);
Panel1.Controls.Add(new LiteralControl("<br/>"));
}
}
}
这种情况首次显示三个label控件,单击按钮将web页面postback回服务器端后,3个页面不再显示