参考以下代码.原理是一样的.
private void Page_Load(object sender, System.EventArgs e)
{ this.CreateTableHead();
for(int i=0;i<this.NewRowCount;i++)
{
this.AddRow();
}}
private void CreateTableHead()
{
Table1.CellPadding = 0;
Table1.CellSpacing = 0;
Table1.Width = Unit.Percentage(100);TableRow trHead = new TableRow();TableCell tcHeadFoodName = new TableCell();
TableCell tcHeadType = new TableCell();
TableCell tcHeadNum = new TableCell();tcHeadFoodName.Text = "食品名称";
tcHeadType.Text = "规格";
tcHeadNum.Text = "数量";trHead.Cells.Add(tcHeadFoodName);
trHead.Cells.Add(tcHeadType);
trHead.Cells.Add(tcHeadNum);
trHead.HorizontalAlign = HorizontalAlign.Center;
trHead.BackColor = Color.AliceBlue;
trHead.Height = 30;Table1.Rows.Add(trHead);
TableRow tr1 = new TableRow();TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
TableCell tc3 = new TableCell();TextBox tb1 = new TextBox();
tb1.ID = "tbFoodName" + Table1.Rows.Count;TextBox tb2 = new TextBox();
tb2.ID = "tbType" + Table1.Rows.Count;TextBox tb3 = new TextBox();
tb3.ID = "tbNum" + Table1.Rows.Count;tc1.Controls.Add(tb1);
tc2.Controls.Add(tb2);
tc3.Controls.Add(tb3);tr1.Cells.Add(tc1);
tr1.Cells.Add(tc2);
tr1.Cells.Add(tc3);tr1.HorizontalAlign = HorizontalAlign.Center;
tr1.Height = 30;
tr1.VerticalAlign = VerticalAlign.Middle;Table1.Rows.Add(tr1);
}private void AddRow()
{
TableRow tr = new TableRow();TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
TableCell tc3 = new TableCell();TextBox tb1 = new TextBox();
tb1.ID = "tbFoodName" + Table1.Rows.Count;TextBox tb2 = new TextBox();
tb2.ID = "tbType" + Table1.Rows.Count;TextBox tb3 = new TextBox();
tb2.ID = "tbNum" + Table1.Rows.Count;tc1.Controls.Add(tb1);
tc2.Controls.Add(tb2);
tc3.Controls.Add(tb3);tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
tr.Cells.Add(tc3);Table1.Rows.Add(tr);}
private int NewRowCount
{get
{
return ViewState["RowCount"] == null?0:(int)ViewState["RowCount"];
}
set
{
ViewState["RowCount"] = value;
}
}
private void btnNewRow_Click(object sender, System.EventArgs e)
{
this.NewRowCount ++;
this.AddRow();}

解决方案 »

  1.   

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["ctlNum"] = 0;
            }
            else
            {
                for (int counter = 0; counter < (int)ViewState["ctlNum"]; counter++)
                {
                    AddControls();
                }
            }
        }
        protected void BtnAnd_Click(object sender, EventArgs e)
        {
            ViewState["ctlNum"] = (int)ViewState["ctlNum"] + 1;
            AddControls();
        }
        private void AddControls()
        {
            CusBox webUserCtrl = (CusBox)Page.LoadControl("~/Control/CusBox.ascx");
            ph.Controls.Add(webUserCtrl);
            setControlsID();
        }
        private void setControlsID()
        {
            int i = 1;
            foreach (CusBox wc in this.ph.Controls)
            {
                wc.ID = i.ToString();
                i++;
            }
        }
      

  2.   

    删除按钮在用户控件里的,remove当前用户控件