我正在更新面板中填充动态控件。步骤是
第 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?