1

在下图中,显示的表格是在单击“创建表格”按钮时动态生成的。

我已将文本框、文件上传、按钮动态添加到表中。

我想通过单击表中的“上传”按钮从文件上传控件上传文件,但我不知道如何处理这些动态生成的控件。

“创建表”按钮的代码如下所示:

protected void Button1_Click(object sender, EventArgs e)
{
    //Button1.Visible = false;
    //Creat the Table and Add it to the Page
    Table table = new Table();
    table.Caption = "Table1";
    table.BackColor = System.Drawing.Color.BurlyWood; 
    Page.Form.Controls.Add(table);
    for (int i = 0; i < 3; i++)
    {
        TableRow row = new TableRow();
        row.BorderStyle = BorderStyle.Ridge;

        for (int j = 0; j <= 10; j++)
        {
            TableCell cell = new TableCell();
            cell.BorderWidth = 5;
            cell.BorderStyle = BorderStyle.Ridge;
            cell.BorderColor = System.Drawing.Color.Azure; 
            for (j = 0; j <= 0; j++)
            {
                Label lbl = new Label();
                lbl.ID = "lblCCRow" + i + "Col" + j;
                lbl.Text = "CC NO. " + i + " ";
                lbl.Width = 100;
                // Add the control to the TableCell
                cell.Controls.Add(lbl);
            }

            for (j = 1; j <= 1; j++)
            {                        
                Label lbl = new Label();
                lbl.ID = "lblRow" + i + "Col" + j;
                lbl.Width = 100;
                lbl.Text = Convert.ToString(DateTime.Now.Day) + "/" + Convert.ToString(DateTime.Now.Month) + "/" + Convert.ToString(DateTime.Now.Year);
                // Add the control to the TableCell
                cell.Controls.Add(lbl);
            }

            for (j = 2; j <= 7; j++)
            {                        
                TextBox tb = new TextBox();
                tb.Width = 100;
                tb.ID = "txtBoxRow" + i + "Col" + j;
                tb.Text = "";
                // Add the control to the TableCell
                cell.Controls.Add(tb);
            }

            for (j = 8; j <= 8; j++)
            {
                FileUpload fileUp = new FileUpload();
                fileUp.ID = "flupRow" + i + "Col" + j;
                fileUp.Width = 220;
                cell.Controls.Add(fileUp);
            }

            for (j = 9; j <= 9; j++)
            {                        
                Button btnUpld = new Button();
                btnUpld.Width = 100;
                btnUpld.ID = "btnUpRow" + i + "Col" + j;
                btnUpld.Text = "Upload";
                cell.Controls.Add(btnUpld);
            }

            for (j = 10; j <= 10; j++)
            {
                Label lbl = new Label();
                lbl.ID = "lblRow" + i + "Col" + j;
                lbl.Text = "[ Status ]";
                lbl.Width = 100;
                // Add the control to the TableCell
                cell.Controls.Add(lbl);
             }

             row.Cells.Add(cell);
         }

         // Add the TableRow to the Table
         table.Rows.Add(row);
     }
     //table.Rows.Add(row);  
 }    
4

2 回答 2

0

附加事件处理程序 btnUpld 运行时。

            Button btnUpld = new Button();
            btnUpld.Width = 100;
            btnUpld.ID = "btnUpRow" + i + "Col" + j;
            btnUpld.Text = "Upload";
             btnUpld.Click += new EventHandler(btnUpld_Click); 
           cell.Controls.Add(btnUpld);



//code behind
private void btnUpld_Click(object sender, System.EventArgs e) 
{
// Add upload functionality here
}
于 2013-03-04T18:45:34.520 回答
0
     btnUpload.Click +=new EventHandler(btnUpload_Click);

使用上面的代码将控件动态添加到表单后添加一个事件处理程序,然后使用以下代码添加创建一个处理此事件的函数

     protected void btnEdit_Click(object sender, EventArgs e)
     {

     }
于 2013-03-04T18:46:04.097 回答