Dynamic button not firing click event or page life cycle for button(not dynamic)

187 views Asked by At

I've got different entity types and depending on which entity is clicked (dropdownlist) the amount and types of uploads which is needed is different every time.

Thus, I am creating multiple dynamic upload controls in a dynamic table with a dynamic upload button to upload all files at the same time (I also tried adding a button on the asp.net page as well). Everything creates fine and I can choose the files to upload.

The problem that I am having is that the dynamic button control is not firing its onclicked event, thus I am not able to save the files. I tried creating a button on the asp.net side which does fire, but because of the page life cycle it's not picking up my upload controls.

I then tried to add the OnInit event and created the dynamic button in there and the rest of the upload dynamic controls on the selected index change of the dropdown, but then the everything gets created except the dynamic upload controls. The click event then fires. (the Page_Init does the same).

Preferably I would like the button not to be dynamic but reach the file upload controls to save the files. Is there a way around the page life cycle that I can achieve this or could anybody please tell me what I am doing wrong? Or how can I get the dynamic button to fire the click event?

Any help will be greatly appreciated....

Here is my code of what I done:

    protected void lstLegalEntity_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (CTflag == false)
        {
            this.Rows = tblRow;
            this.Columns = tblCol;

            lblAmountOfRows.Text = "4";
            CreateDynamicTable();
        }
        else
        {
            CTflag = true;
        }

        clrControls();
    }

    protected void CreateDynamicTable()
    {
        string filterstring = "";
        filterstring = "SELECT let.ID, UploadType " +
                        "FROM [dbo].[AssetContract_LegalEntityLinks] lel " +
                        "INNER Join [dbo].[AssetContract_LegalEntity] le " +
                        "ON lel.LegalEntityRef = le.ID " +
                        "INNER JOIN [dbo].[AssetContract_LegalEntityTypes] let " +
                        "ON lel.LegalTypeRef = let.ID " +
                        "WHERE lel.LegalEntityRef = @LegalEntityRef ";

        cn = new SqlConnection(GetConnectionString());
        SqlCommand myCmd = new SqlCommand();
        myCmd.CommandText = filterstring;
        myCmd.Connection = cn;
        myCmd.CommandType = CommandType.Text;

        if (lstLegalEntity.SelectedValue != "")
        {
            myCmd.Parameters.AddWithValue("@LegalEntityRef", Convert.ToInt32(lstLegalEntity.SelectedValue));
        }
        else
        {
            myCmd.Parameters.AddWithValue("@LegalEntityRef", Convert.ToInt32(0));
        }

        cn.Open();
        SqlDataReader myReader = myCmd.ExecuteReader();

        tblRow = GetUploadControlsCount();

        if (CTflag == false)
        {
            table.Caption = "Upload Requirements";
            table.ID = "Upload Requirements";
            table.BackColor = System.Drawing.Color.BurlyWood;
            divFileUploads.Controls.Add(table);

            for (i = 0; i < 1; i++)
            {
                row = new TableRow();
                row.BorderStyle = BorderStyle.Ridge;

                for (j = 0; j <= tblCol; j++)
                {
                    cell = new TableCell();
                    cell.BorderWidth = 5;
                    cell.BorderStyle = BorderStyle.Ridge;
                    cell.BorderColor = System.Drawing.Color.Azure;
                    for (j = 0; j <= tblCol; j++)
                    {
                        string[] Header = { "Upload Type", "File" };
                        Label lbl = new Label();
                        lbl.ID = "lblHeader" + j;
                        if (j == 1)
                        {
                            lbl.Width = 220;
                        }
                        else
                        {
                            lbl.Width = 100;
                        }
                        lbl.Text = Header[j];

                        cell.Controls.Add(lbl);
                    }
                    row.Cells.Add(cell);
                }

                table.Rows.Add(row);
            }

            int readCount = 1;
            while (myReader.Read())
            {

                for (i = 0; i < 1; i++)
                {
                    row = new TableRow();
                    row.ID = "rw" + myReader["UploadType"].ToString();
                    row.BorderStyle = BorderStyle.Ridge;

                    for (j = 0; j <= tblCol; j++)
                    {
                        cell = new TableCell();
                        cell.ID = tbColId + i + j + myReader["UploadType"].ToString(); ;
                        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 + myReader["UploadType"].ToString();
                            lbl.Text = myReader["UploadType"].ToString();
                            lbl.Width = 100;
                            // Add the control to the TableCell
                            cell.Controls.Add(lbl);
                        }

                        for (j = 0; j < 1; j++)
                        {
                            fileUp = new FileUpload();
                            //m = i; n = j;
                            fileUp.ID = filename + i + readCount.ToString();
                            fileUp.Width = 350;
                            cell.Controls.Add(fileUp);
                            cmdArg = fileUp.ID;
                        }

                        row.Cells.Add(cell);
                    }

                    table.Rows.Add(row);
                    readCount++;
                } 

                i = 0;
                j = 0;
            }

            for (i = 0; i < 1; i++)
            {
                rrow = new TableRow();
                rrow.ID = "ResultRow";
                rrow.BorderStyle = BorderStyle.Ridge;
                rrow.HorizontalAlign = HorizontalAlign.Center;

                for (j = 0; j <= tblCol; j++)
                {
                    rcell = new TableCell();
                    rcell.ID = "resultCol" + j;
                    rcell.BorderWidth = 5;
                    rcell.BorderStyle = BorderStyle.Ridge;
                    rcell.BorderColor = System.Drawing.Color.Azure;

                    for (j = 0; j < 1; j++)
                    {
                        btnUpload = new Button();
                        btnUpload.Width = 100;
                        btnUpload.Text = "Upload";
                        btnUpload.ID = btnUpload.Text;
                        rcell.Controls.Add(btnUpload);
                        btnUpload.Click += new EventHandler(UpLdButton_Click);
                    }

                    rrow.Cells.Add(rcell);
                }
                table.Rows.Add(rrow);
            }

            CTflag = true;
            ViewState["dynamictable"] = true;

            cn.Close();
            myReader.Close();
        }
    }

    protected void UpLdButton_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < tblRow; i++)
        {
            fileUp = (FileUpload)FindControlRecursive(this, string.Format("fileUpLoader{0}{1}", 0, i)); 
        //rest of code to save file
        }
    }
2

There are 2 answers

0
Elisheva Wasserman On BEST ANSWER

Why don't you use asp repeater to display FileUploads?

you can easily set the html template and design, as DataSource use DataTable of your data

<asp:repeater id='rp' runat='server'>
<ItemTemplate>
 <%Eval("UploadType"%>   -this is the caption

   <asp:fileUpload id='file' runat='server'/>
   <asp:Button id='btnUpload' onclick='UploadClick' runat='server'/>

</ItemTemplate>

protected void lstLegalEntity_SelectedIndexChanged(object sender, EventArgs e)
{
     //get data into DataTable
       rp.DataSource=dt;
       rp.DataBnd();
}


  protected void UpLdButton_Click(object sender, EventArgs e)
{
    for (int i = 0; i < tblRow; i++)
    {
         //this way you get the relevant fileUpload
        fileUp = (FileUpload)((Button)Sender).Parent.FindControl ("file);

    //rest of code to save file
    }
}
1
Gridly On

You were on the right track when you added the dynamic buttons in the OnInit event - you just needed to also add the FileUpload controls too.

All of the controls need to be recreated on every postback. .NET automatically handles the controls you added to the ASPX page. You are responsible for the dynamic controls you add programmatically.

Here are a couple of approaches you can take from where you are:

  1. Continue with dynamically adding the control but make the change so you are adding all of the controls, not just the buttons.

  2. If you have a known maximum to the number of controls then you could add then all and control what is displayed using the Visible property. This works when the number of controls is small. It looks like you are putting borders around the table cells so this would not look so good in your case. If you can change the layout so that hiding the controls does not result in residual artifacts then this is an option.