我现在想动态的在页面上加入一些button,label和radiobuttonlist等等的一些控件。这些控件我用一个placeholder在我的代码里按需要一个个的添加进去。
页面显示没有问题。但是当我提交表单的时候发现,placeholder不能保存我加入的控件,也就是说填写的表单内容都不复存在。
我尝试将这些控件赋值为static的,但当它提交时,校验viewstate出现了问题。(MAC的问题)
求高人指点如果才能是我动态添加的控件在页面提交之后(postback),还能保持它的状态!

解决方案 »

  1.   

    将placeholder加载控件的代码放到page_load中,
    使得每次加载页面都能执行
      

  2.   

    private void LoadUserControls()
    {
    DataTable dt = new DataTable();
    dt = this.GetPropertyItem(obj);

    int i = 0;
    if(this.TableLabel.Controls.Count <= 0)
    {
    foreach(DataRow dr in dt.Rows)
    {
    TableRow tableRowLabel = new TableRow();
    TableCell tableCellLabel = new TableCell(); tableRowLabel.ID = "TablerowLabel_" + i;
    tableCellLabel.ID = "tableCellLabel_" + i; Label labelName = new Label();
    labelName.ID = "lable_" + dr["ItemEName"].ToString();
    labelName.Text = dr["ItemCName"].ToString() + ":"; tableCellLabel.Controls.Add(labelName);
    tableRowLabel.Cells.Add(tableCellLabel);
    this.TableLabel.Controls.Add(tableRowLabel); TableRow tableRowTextBox = new TableRow();
    TableCell tableCellTextBox = new TableCell(); tableRowTextBox.ID = "TablerowTextBox_" + i;
    tableCellTextBox.ID =  "tableCellTextBox_" + i; TextBox textBoxName = new TextBox();
    textBoxName.ID = dr["ItemEName"].ToString();
    textBoxName.Text = "";
    textBoxName.BorderStyle=BorderStyle.Groove; tableCellTextBox.Controls.Add(textBoxName);
    tableRowTextBox.Cells.Add(tableCellTextBox);
    this.TableTextBox.Controls.Add(tableRowTextBox); i++;
    }
    }
    DataTable dt = this.GetPropertyItem(obj);
    for(int i=0;i< this.TableTextBox.Controls.Count;i++)
    {
    TableRow tableRowTextBox = (TableRow)this.TableTextBox.FindControl("TablerowTextBox_"+i);
    TableCell tableCellTextBox = (TableCell)tableRowTextBox.FindControl("tableCellTextBox_"+i);
    TextBox tb = (TextBox)tableCellTextBox.FindControl(dt.Rows[i]["ItemEName"].ToString());tb.Text = dr[dt.Rows[i]["ItemEName"].ToString()].ToString();
    }
      

  3.   

    http://www.cnblogs.com/lovecherry/archive/2005/05/10/152455.html
    http://www.cnblogs.com/lovecherry/archive/2005/04/09/134543.html
    http://www.cnblogs.com/lovecherry/archive/2005/04/16/138968.html