2013-03-04 32 views
1

在下图中,所显示的表是通过点击“创建表”按钮动态生成的。我可以处理动态生成的控件

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

我想从表上点击“上传”按钮上传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); 
}  

回答

0

附上事件handeler 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 
} 
+0

日Thnx苏雷什处理此事件的函数: )这非常有帮助 – 2013-03-06 08:37:45

+0

欢迎Nishant。 – SP007 2013-03-07 15:27:57

0
 btnUpload.Click +=new EventHandler(btnUpload_Click); 

动态地添加所述控制到窗体,使用上述代码之后添加一个事件处理程序,然后添加创建使用以下代码

 protected void btnEdit_Click(object sender, EventArgs e) 
    { 

    } 
相关问题