我通过后台代码动态的向前台添加了控件,代码如下:
        ds = DbTools.getDataSetBySQL("select distinct substring(inform_type,1,1) inform_type_order_id from dim_inform_type order by 1");
        GV_inform.DataSource = ds;
        GV_inform.DataBind();
        foreach (GridViewRow row in GV_inform.Rows)
        {
            PlaceHolder PH_content = (PlaceHolder)row.FindControl("PH_content");
            string inform_type_order_id = ((ITextControl)row.FindControl("L_inform_type_order_id")).Text;
            ds = DbTools.getDataSetBySQL("select * from dim_inform_type where inform_type like '" + inform_type_order_id + "%'");
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    CheckBox cb = new CheckBox();
                    cb.ID = ds.Tables[0].Rows[i]["inform_type"].ToString();
                    cb.Text = ds.Tables[0].Rows[i]["inform_desc"].ToString();
                    PH_content.Controls.Add(cb);
                    PH_content.Controls.Add(new LiteralControl(" "));
                }
            }
        }
 可是页面一刷新这些动态添加的控件就消失了,我也无法把控件的修改值,穿到后台去,请问哪位大侠有方法解决?

解决方案 »

  1.   

    一般来说,如果是添加服务器端的server控件不应该直接ADD,而应该这样
    TextBox tx = new TextBox();
    tx.ID = "textbox";
    this.Form.Controls.Add(tx);
    而获取这个服务器端的客户端ID来处理事件应该是
    tx.ClientID.ToString();
      

  2.   

    怎么通过
    tx.ClientID.ToString();
    找到啊?创建以后一刷新不就没有了吗?
      

  3.   

    调一下 TrackViewState()
    试一下
      

  4.   

    我看了我们公司代码中解决刷新后控件消失的方法是,没有ispostback的判断,每次都加载。
      

  5.   

    strife() 的答案是正解,但我还是不明白,如果每次都重新加载,为什么还能保持控件的状态呢?