刚动态添加的那些控件,在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有关系!但具体什么关系!俺不知道!

解决方案 »

  1.   

    viewstate保存控件的状态怎么样啊?我只是这样想的,不过没用过
      

  2.   

    动态生成的控件是不能保持 ViewState 的.
      

  3.   

    NekChan(小猪)
    动态生成的控件是不能保持 ViewState 的.有这样的说法啊????那我全部得从头做了!!
    您遇见这种情况怎么解决的呀,给一些提示也好哇
      

  4.   

    感谢
    [动网]slightboy,wyd1520,banmuhuangci;
    [经典]flash665,yangliwei,7421021 ,martin0728
    [CSDN]seawolflover,jxf654,NekChan(骑蜗牛看星星),bestshl(快乐的Coder),connynet(暗目),XIEWH,rroo(天之痕) ,chxtp,
    [百度知道]_冻结_ ,drippy,xiangpi_ws
    [QQ]★.凍結~(51076008),深灰色(350763343)
    正确答案Default.aspx.csusing System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page
    {
        //全局静态的链表
        public static ArrayList list = new ArrayList();    protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void Button1_Click(object sender, EventArgs e)
        {
            //添加
            list.Clear();
            for (int i = 0; i < Convert.ToInt32(TextBox1.Text); i++)
            {
                TextBox t = new TextBox();
                t.ID = "text" + i;
                list.Add(t);
                Panel1.Controls.Add(t);
            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            //取值
            string s = "";
            for (int i = 0; i < list.Count; i++)
            {
                TextBox TextBox1 = (TextBox)list[i];
                s += "第" + (i + 1).ToString() + "个文本框的值是:" + Request[TextBox1.ID] + "\r\n";
            }        TextBox2.Text = s;
        }}Default.aspx<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
    <form id="form1" runat="server">
        <div>
            生成控件的个数:<asp:TextBox ID="TextBox1" runat="server">1</asp:TextBox> 
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="生成" />
            <asp:Panel ID="Panel1" runat="server" BackColor="#E0E0E0" Height="173px" Width="545px">
            </asp:Panel>
       
        </div>
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="取值" />
            <br />
            <asp:TextBox ID="TextBox2" runat="server" Height="211px" TextMode="MultiLine" Width="538px"></asp:TextBox>
        </form></body>
    </html>WebUserControl.ascx<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>WebUserControl.ascx.csusing System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class WebUserControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
    }
    主要用到了静态连接表甭管怎么说,这个问题难我一个春节。。记录一下现在的心情~哼哼哈嘿
      

  5.   

    可是点击button2的时候,控件还是不见了呀