0

我正在更新面板中填充动态控件。步骤是

第 1 步:动态控件填充在动态表中,就像在面板内

<asp:Panel ID="pnlShowDDF" runat="server" Visible="False" ViewStateMode="Enabled">
</asp:Panel>

第2步:

         protected void loadTable()
            {
                HtmlTable tblDDF = new HtmlTable();
                var objDDF = new ddf();

            var dsDdfDetail = "DataSet Loaded"
            if (dsDdfDetail.Tables[0].Rows.Count > 0)
            {
                int RowsCount = dsDdfDetail.Tables[0].Rows.Count;
                for (int i = 0; i < RowsCount; i++)
                {
                    HtmlTableRow tblNewRow = new HtmlTableRow();
                    HtmlTableCell tblDdfCell = new HtmlTableCell();
                    tblDdfCell1.Controls.Add(addCheckbox(dsDdfDetail.Tables[0].Rows[i][0].ToString()));
    //The addCheckbox function returns the checkbox with its text


        tblNewRow.Controls.Add(tblDdfCell);
                    tblDDF.Controls.Add(tblNewRow);
                }
            HtmlTableRow htFooterRow = new HtmlTableRow();
            HtmlTableCell htFooterCell = new HtmlTableCell();

            htFooterCell.Controls.Add(DelButton());
//DelButton() is written in below
                htFooterCell.Attributes.Add("class", "pnlFooterRow");
                htFooterCell.ColSpan = 2;
                htFooterRow.Cells.Add(htFooterCell);
                tblDDF.Controls.Add(htFooterRow);
                }
            pnlShowDDF.Controls.Add(tblDDF);
            pnlShowDDF.Visible = true;
        }
protected Button DelButton()
    {
        var btnDelete = new Button();
        btnDelete.ID = "btnDelete";
        btnDelete.Text = "De-Allocate";
        btnDelete.Click += new EventHandler(btnDelete_Click);
        btnDelete.Attributes.Add("class", "button");
        btnDelete.ViewStateMode = ViewStateMode.Enabled;
        return btnDelete;
    }

第 3 步需要从 btnDelete 访问动态复选框 id

void btnDelete_Click(object sender, EventArgs e)
    {
        //Need to access the checkbox id's here
        foreach(Control chk in pnlShowDDF.Controls)
        {
            if(chk is CheckBox)
            {
               CheckBox chkbx= chk as CheckBox;
                if(chkbx.Checked)
                {
                   //Here i need to access the id's which i can't right now
                }
            }
        }
    }

第 4 步:我在 OnInit 上调用了 loadTable 函数,但没有任何收获

protected override void OnInit(EventArgs e)
    {
       base.OnInit(e);
        loadTable();
    }

应该怎么做才能访问复选框ID?

4

1 回答 1

0

每当您添加动态控件时,您必须在Page_Init事件中添加它们并为其分配适当的 ID,以便在发生回发时,这些动态添加的控件连同它们的值一起被再次创建,并且在您的情况下可以在其他事件中访问btnDelete_Click

因此,您缺少的是为表格单元格和行分配 ID,我希望您将 ID 分配给复选框。

========================== 编辑======================== ====

这是与我一起工作的代码,其中btnDelete_Click有足够好的更改。我得到了我添加的复选框,如果我选中其中任何一个,我得到的值为 true。

protected void loadTable()
    {
        HtmlTable tblDDF = new HtmlTable();
        //var objDDF = new ddf();

        //DataSet dsDdfDetail = new DataSet();
        //if (dsDdfDetail.Tables[0].Rows.Count > 0)
        //{
            //int RowsCount = dsDdfDetail.Tables[0].Rows.Count;
            for (int i = 0; i < 5; i++)
            {
                HtmlTableRow tblNewRow = new HtmlTableRow();
                HtmlTableCell tblDdfCell = new HtmlTableCell();
                tblDdfCell.Controls.Add(addCheckbox(i.ToString()));
                //The addCheckbox function returns the checkbox with its text


                tblNewRow.Controls.Add(tblDdfCell);
                tblDDF.Controls.Add(tblNewRow);
            }
            HtmlTableRow htFooterRow = new HtmlTableRow();
            HtmlTableCell htFooterCell = new HtmlTableCell();

            htFooterCell.Controls.Add(DelButton());
            //DelButton() is written in below
            htFooterCell.Attributes.Add("class", "pnlFooterRow");
            htFooterCell.ColSpan = 2;
            htFooterRow.Cells.Add(htFooterCell);
            tblDDF.Controls.Add(htFooterRow);
        //}
        pnlShowDDF.Controls.Add(tblDDF);
        pnlShowDDF.Visible = true;
    }
protected Button DelButton()
    {
        var btnDelete = new Button();
        btnDelete.ID = "btnDelete";
        btnDelete.Text = "De-Allocate";
        btnDelete.Click += new EventHandler(btnDelete_Click);
        btnDelete.Attributes.Add("class", "button");
        //btnDelete.ViewStateMode = ViewStateMode.Enabled;
        return btnDelete;
    }
protected CheckBox addCheckbox(string id)
    {
        CheckBox chk = new CheckBox();
        chk.ID = id;
        return chk;
    }
void btnDelete_Click(object sender, EventArgs e)
    {
        //Need to access the checkbox id's here
        foreach (Control chk in pnlShowDDF.Controls)
        {
            if (chk is HtmlTable)
            {
                HtmlTable tbl = (HtmlTable)chk;

                foreach (HtmlTableRow row in tbl.Rows)
                {
                    foreach (HtmlTableCell cell in row.Cells)
                    {
                        foreach (Control chk1 in cell.Controls)
                        {
                            if (chk1 is CheckBox)
                            {
                                CheckBox chkbx = chk1 as CheckBox;
                                if (chkbx.Checked)
                                {
                                    //Here i need to access the id's which i can't right now
                                }
                            }
                        }
                    }
                }
            }
        }
    }
于 2012-09-11T07:31:44.003 回答