日期:2014-05-18  浏览次数:20559 次

还是table增加行的问题
asp.net里,使用web 控件 table,动态增加行,为什么只能增加一行?第一次点击button按钮是可以增加一行的,但第二次点击button按钮就不能增加行了?这是什么原因呢?顺便贴上代码:

C# code

        /// <summary>
        /// 加载Table(根据参数加载行数)
        /// </summary>
        /// <param name="iRow">需要加载的行数</param>
        private void InitTable(int iRow, int iTable)
        {
              TableCell td;
            for (int i = 0; i < iRow; i++)
            {

                TableRow tr = new TableRow();

                td = new TableCell();
                td.ID = "ColId" + i + 1;                    //设置第一个单元格的ID
                td.Text = (iTable + i).ToString();            //设置第一个单元格文本
                td.Width = Unit.Pixel(60);
                tr.Cells.Add(td);

                //设置第二个单元格的ID和单元格文本  提交时间
                td=new TableCell();
                td.ID = "ColDate" + i + 1;
                td.Text = DateTime.Now.ToString("yyyy-MM-dd");
                td.Width = Unit.Point(120);
                tr.Cells.Add(td);

                //设置第三个单元格的ID和单元格文本  提交人
                td = new TableCell();
                td.ID = "ColAuthor" + i + 1;
                TextBox txtAuthor = new TextBox();
                txtAuthor.Width = Unit.Pixel(120);
                txtAuthor.ID = "txtAuthor" + i.ToString();
                td.Controls.Add(txtAuthor);
                td.Width = Unit.Point(120);
                tr.Cells.Add(td);

                //设置第四个单元格的ID和单元格文本  金额
                td = new TableCell();
                td.ID = "ColMoney" + i + 1;
                TextBox txtMoney = new TextBox();
                txtMoney.ID = "txtMoney" + i.ToString();
                txtMoney.Width = Unit.Pixel(100);
                td.Controls.Add(txtMoney);
                td.Width = Unit.Point(100);
                tr.Cells.Add(td);

                //设置第五个单元格的ID和单元格文本  审核依据
                td = new TableCell();
                td.ID = "ColPayment" + i + 1;
                TextBox txtPayment = new TextBox();
                txtPayment.ID = "txtPayment" + i.ToString();
                txtPayment.Width = Unit.Pixel(200);
                td.Controls.Add(txtPayment);
                td.Width = Unit.Point(200);
                tr.Cells.Add(td);


                Table1.Rows.Add(tr);

            }
 
            iRow = Table1.Rows.Count;
        }

/// <summary>
        /// 新增行按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            int irow = Convert.ToInt32(ViewState["iRow"]);
            irow = int.Parse(txt1.Text);

            InitTable(1, irow);

                    }



------解决方案--------------------
http://topic.csdn.net/u/20120213/11/cb1d3efd-7b8d-4200-bc9f-9b4aef49813f.html
------解决方案--------------------
我的理解是:
你每点击一次新增按钮,页面就会刷新一次,上次添加的那行数据就丢失了,然后是在你页面初始的状态下的数据基础上添加了一行,所以从效果上看,是后来再点击就不增加了。
如果是你点击几次就增加几行,你可以利用好你的第一个参数,别让它写死为1.
------解决方案--------------------
js完全可以实现,更加没有必要去勾画Table