动态按钮不触发按钮的单击事件或页面生命周期(非动态)

时间:2015-06-24 12:35:33

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

我有不同的实体类型,并且根据点击的实体(下拉列表),每次所需的上传量和类型都不同。

因此,我在动态表中创建了多个动态上传控件,并使用动态上传按钮同时上传所有文件(我也尝试在asp.net页面上添加一个按钮)。一切都很好,我可以选择要上传的文件。

我遇到的问题是动态按钮控件没有触发其onclicked事件,因此我无法保存文件。我尝试在asp.net端创建一个按钮,但是由于页面生命周期,它没有启动我的上传控件。

然后我尝试添加OnInit事件并在其中创建动态按钮,其余的上传动态控件对所选索引更改下拉列表,但随后创建的所有内容除了动态上传控件外。然后点击事件将触发。 (Page_Init也是如此)。

最好我希望按钮不是动态的,而是到达文件上传控件来保存文件。有没有办法绕过页面生命周期,我可以实现这一点,或者任何人请告诉我我做错了什么?或者如何让动态按钮触发点击事件?

任何帮助将不胜感激....

以下是我所做的代码:

    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 个答案:

答案 0 :(得分:1)

当您在OnInit事件中添加动态按钮时,您处于正确的轨道 - 您还需要添加FileUpload控件。

每次回发都需要重新创建所有控件。 .NET自动处理您添加到ASPX页面的控件。您负责以编程方式添加的动态控件。

以下是您可以从以下几个方面采取的一些方法:

  1. 继续动态添加控件但进行更改,以便添加所有控件,而不仅仅是按钮。

  2. 如果您有一个已知的最大控件数,那么您可以添加all并使用Visible属性控制显示的内容。这在控件数量很少时有效。看起来你在表格单元格周围放置边框,所以在你的情况下这看起来不太好。如果您可以更改布局以便隐藏控件不会导致残留伪影,那么这是一个选项。

答案 1 :(得分:1)

为什么不使用asp转发器来显示FileUploads?

您可以轻松设置html模板和设计,因为DataSource使用数据的DataTable

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