【问题标题】:Dynamic button not firing click event or page life cycle for button(not dynamic)动态按钮未触发按钮的单击事件或页面生命周期(非动态)
【发布时间】:2015-06-24 12:35:32
【问题描述】:

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

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

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

然后我尝试添加 OnInit 事件并在其中创建动态按钮,并在下拉列表的选定索引更改上创建其余的上传动态控件,但随后创建了除动态上传控件之外的所有内容。然后触发 click 事件。 (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
        }
    }

【问题讨论】:

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


    【解决方案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
        }
    }
    

    【讨论】:

      【解决方案2】:

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

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

      以下是您可以采取的几种方法:

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

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

      【讨论】:

      • 感谢@Gridly 的建议。问题是,必须从下拉列表中选择项目并检索数据,然后才能创建文件上传......上传的数量也会有所不同,特别是因为可以动态创建具有上传类型的新实体并且系统需要能够处理这个......我创建了一个中继器,它可以解决问题:) 谢谢
      猜你喜欢
      • 1970-01-01
      • 2020-12-28
      • 1970-01-01
      • 2014-11-11
      • 2010-12-19
      • 2015-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多