我可以处理动态生成的控件

时间:2013-03-04 18:40:37

标签: asp.net file-upload c#-3.0 dynamic-controls

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

我已将textbox,fileupload,Buttons动态添加到表中。

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

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

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);  
 }    

2 个答案:

答案 0 :(得分:0)

附加事件处理程序btn上传运行时。

            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
}

答案 1 :(得分:0)

     btnUpload.Click +=new EventHandler(btnUpload_Click);

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

     protected void btnEdit_Click(object sender, EventArgs e)
     {

     }