Вопрос

In the following image the table displayed is generated dynamically on click of “Create Table” button.

I have added textboxes, fileupload, Buttons dynamically to the table.

I want to upload files from fileupload control on click of “Upload” button in the Table, but I have no idea how to handle those Dynamically generated Controls.

Code of “Create Table” button goes like this :

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);  
 }    
Это было полезно?

Решение

Attach event handeler btnUpld runtime.

            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
}

Другие советы

     btnUpload.Click +=new EventHandler(btnUpload_Click);

add an event handler after adding the control dynamically to the form, using the above code and then add create a function that handles this event using the following code

     protected void btnEdit_Click(object sender, EventArgs e)
     {

     }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top