需要访问更新面板中的动态控件ID

时间:2012-09-11 05:50:19

标签: c# asp.net page-lifecycle

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

步骤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?

1 个答案:

答案 0 :(得分:0)

无论何时添加动态控件,都必须在Page_Init事件中添加它们并为它们分配正确的ID,以便在发生回发时,再次创建这些动态添加的控件及其值,并且可以在其他事件,在您的情况下btnDelete_Click

所以你缺少的是为表格单元格和行分配ID,我希望你为复选框分配ID。

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

以下是与我合作的代码,btnDelete_Click已经有了足够好的变化。我收到了我添加的复选框,如果我检查其中的任何一个,我得到的值为真。

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