如何对动态生成的控件进行操作。
动态生成控件功能已经实现。只是在执行其它操作的同时页面刷新(点击其它Button按钮时)会隐藏掉先前生成的控件。
使用this.Controls.Count也无法找到,请问如何解决此问题。贴出动态生成控件的方法
protected void BindParametersInfo(string strDeviceTypeID,Panel tempPanel) {        int Cyci=0;
        DataTable tempDT=new DataTable();
        HtmlTable tempHtmlTable=new HtmlTable();
        tempHtmlTable.Width="100%";        try {            strSql=string.Empty;
            strSql="";
            tempDT=DataProcessing.Select(strSql).Tables[0];            while (Cyci<tempDT.Rows.Count) {                HtmlTableRow tempHtmlTableRow=new HtmlTableRow();                if (Cyci>=tempDT.Rows.Count) break;                HtmlTableCell tempHtmlTableCell1=new HtmlTableCell();
                tempHtmlTableCell1.Width="100";
                Label tempLB1=new Label();
                tempLB1.ID="LB_"+Cyci.ToString();
                tempLB1.Text=tempDT.Rows[Cyci]["ParametersName"].ToString();
                tempLB1.Width=98;
                tempLB1.Visible = true;
                tempHtmlTableCell1.Controls.Add(tempLB1);
                tempHtmlTableCell1.Align="right";
                tempHtmlTableRow.Cells.Add(tempHtmlTableCell1);                HtmlTableCell tempHtmlTableCell2=new HtmlTableCell();
                tempHtmlTableCell2.Width="187";
                TextBox tempBox1=new TextBox();
                tempBox1.ID="TBox_"+Cyci.ToString();
                tempBox1.Visible=true;
                tempBox1.Text=tempDT.Rows[Cyci]["ParametersName"].ToString();
                tempBox1.Width=183;
                tempHtmlTableCell2.Controls.Add(tempBox1);
                tempHtmlTableCell2.Align="left";
                tempHtmlTableRow.Cells.Add(tempHtmlTableCell2);                Cyci++;
                if (Cyci>=tempDT.Rows.Count) break;                HtmlTableCell tempHtmlTableCell3=new HtmlTableCell();
                tempHtmlTableCell3.Width="100";
                Label tempLB2=new Label();
                tempLB2.Visible = true;
                tempLB2.ID="LB_"+Cyci.ToString();
                tempLB2.Text=tempDT.Rows[Cyci]["ParametersName"].ToString();
                tempLB2.Width=98;
                tempHtmlTableCell3.Controls.Add(tempLB2);
                tempHtmlTableCell3.Align="right";
                tempHtmlTableRow.Cells.Add(tempHtmlTableCell3);                HtmlTableCell tempHtmlTableCell4=new HtmlTableCell();
                tempHtmlTableCell4.Width="187";
                TextBox tempBox2=new TextBox();
                tempBox2.ID="TBOX_"+Cyci.ToString();
                tempBox2.Visible = true;
                tempBox2.Text=tempDT.Rows[Cyci]["ParametersName"].ToString();
                tempBox2.Width=183;
                tempHtmlTableCell4.Controls.Add(tempBox2);
                tempHtmlTableCell4.Align="left";
                tempHtmlTableRow.Cells.Add(tempHtmlTableCell4);                tempHtmlTable.Rows.Add(tempHtmlTableRow);
                Cyci++;
            }
            tempPanel.Controls.Add(tempHtmlTable);
        } catch {
        }
    }

解决方案 »

  1.   

    编程逻辑可以参考帖子《动态添加多个btn的问题,麻烦帮我解决一下
      

  2.   

    既然是动态生成,那在postback后就会消失,应该在pageload中始终绑定此控件。
      

  3.   

    在Page_Init事件中初始化控件的话,postback就不会消失,你在baidu里面找找关于Page_Init的文章,这个方法会在Page_Load之前执行。
      

  4.   

    刷了之后需要在此绑定,应该在pageload中始终绑定此控件
      

  5.   

    每次刷新都需要重新绑定生成的控件是一方面,另外,9楼说的page_init里加绑定的控件是会被viewstate的,不会丢失你在page_init事件里生成控件试试看
      

  6.   

    在Page_Load里调用动态生成函数可以实现,但不符合业务要求。这里通过其它方法实现了。谢谢各位支持。
      

  7.   

    把动态生成控件代码放到protected override void OnInit(EventArgs e)
    {}
      

  8.   

    loadviewstate里还原状态,或者在page_load也可以,
      

  9.   

    动态生成控件,
    要确保你在每次page_load时都执行生成控件的代码