关键地方在于要考虑到夜间生命周期的问题,不要把获取值在放在页面第一次加载中去就OK了
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//for (int i = 0; i < 5; i++)
//{
// TextBox txt = new TextBox();
// txt.ID = "txt" + i;
// txt.Text = i.ToString();
// this.container.Controls.Add(txt);
//}
}
for (int i = 0; i < 5; i++)
{
TextBox txt = new TextBox();
txt.ID = "txt" + i;
txt.Text = i.ToString();
this.container.Controls.Add(txt);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string txt = String.Empty;
for(int i=0;i<5;i++)
{
TextBox tb = this.container.FindControl("txt" + i) as TextBox;
txt += tb.Text + "/";
}
this.Label1.Text = "自动生成的控件的值为:" + txt;
//this.Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('" + txt + ")", true);
}
}