刚动态添加的那些控件,在IsPostBack为true时,又自动消失了,
刚动态添加的那些控件,在IsPostBack为true时,又自动消失了,
在某一个按钮里动态添加了几个控件,
然后在该页再点提交时,这些控件又都消失了,于是取不值!
贴点代码片断
if (IsPostBack)
{
for (int i = 0; i < 8; i++)
{
Control c = Page.LoadControl( "WebUserControl.ascx ");
this.Panel1.Controls.Add(c);
}
}
-------------------------
foreach (Control c in this.Panel1.Controls)
{
//如果为用户控件,则取值,当前容器Panel1
if (c.GetType().Name.Equals( "webusercontrol_ascx "))
{
//查找DropDownList1,当前容器webusercontrol_ascx
//住宿
DropDownList ddl = (DropDownList)c.FindControl( "DropDownList1 ");
oTotal_Hotel += ddl.SelectedValue.ToString() + ", ";
//早餐
DropDownList ddl_breakfast = (DropDownList)c.FindControl( "DropDownList2 ");
breakfast += ddl_breakfast.SelectedValue.ToString() + ", ";
//午餐
DropDownList ddl_lunch = (DropDownList)c.FindControl( "DropDownList3 ");
lunch += ddl_lunch.SelectedValue.ToString() + ", ";
//晚餐
DropDownList ddl_dinner = (DropDownList)c.FindControl( "DropDownList4 ");
dinner += ddl_dinner.SelectedValue.ToString() + ", ";
}
else
{
//Response.Write(c.GetType().Name+ "出错了,请与管理员联系 ");
//Response.End();
}
}
Response.Write( "住宿: " + oTotal_Hotel + " <hr> ");
Response.Write( "早餐: " + breakfast + " <hr> ");
Response.Write( "午餐: " + lunch + " <hr> ");
Response.Write( "晚餐: " + dinner + " <hr> ");
-------------------
这两段代码分别放在
protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
和
protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
中,
求解!
一,不让它消失,
二,取它的值,我想如果不消失就按现在的办法就可以取值了,
PS:如果放在Page_Load里就没事儿,我想跟IsPostBack有关系!但具体什么关系!俺不知道!
[解决办法]
viewstate保存控件的状态怎么样啊?我只是这样想的,不过没用过
[解决办法]
up
[解决办法]
动态生成的控件是不能保持 ViewState 的.