I had created dynamic checkboxes and added them to the table on asp.net
Here is my code:
**Creating checkbox:**
TableCell tc = new TableCell();
CheckBox cb = new CheckBox();
TableRow tr;
// maintaining only 3 cells per row
if (num3 % 3 == 0)
{
tr = new TableRow();
tblImage.Rows.Add(tr);
}
else
tr = tblImage.Rows[num3 / 3];
// creating checkbox
cb.ID = "cb" + (num3 + 1);
cb.Text = "Page " + (num3 + 1);
// adding to table cell
tc.Controls.Add(cb);
tr.Cells.Add(tc);
现在,在按钮点击事件(服务器端)上,我想获取复选框ID,其中复选框已选中如何操作?
**Button Click Event:**
在这种情况下,我总是将行计为零。不确定原因。即使它有2行&总共6个细胞。
//getting rows count to check
int cnt = tblImage.Rows.Count;
// Always getting zero, even though there are 2 rows
//looping through each row
foreach (TableRow tr in tblImage.Rows)
{
//looping through each cell
foreach (TableCell tc in tr.Cells)
{
// looping for controls
foreach (Control ctr in tc.Controls)
{
// checking for a checkbox
var cb = ctr as CheckBox;
if (cb != null)
{
//getting check box id & status
Response.Write(cb.ID+" State: "+cb.Checked+"<br/>");
}
}
}
}
In this button click event, the table rows count is always showing as 0.
答案 0 :(得分:-1)
foreach(Control ctrl in tc.Controls)
{
if(ctrl is CheckBox)
{
CheckBox chkBox= ctrl as CheckBox ;
if(chkBox.Checked == true)
// do something.
}
}