这里我需要在asp.net web应用程序的动态网格中动态创建复选框,我使用以下代码来理解,然后我需要使用Find Control检索这些Id - {{1} } 但显示错误消息,如
空引用(对象引用未设置为对象的实例)
所以请帮我解决这个问题。
((CheckBox)GrdShiftDetails.FindControl(Convert.ToString(vStrchkboxId))).Checked
答案 0 :(得分:0)
添加
if(GrdShiftDetails.Rows.Count > 0 && GrdShiftDetails.Columns.Count > 0)
{
// your code
(GrdShiftDetails.Rows[VintCurrentRow].Cells[coloumcount].Text != "00:00-00:00") {
CheckBox chk = new CheckBox();
chk.ID = "chk" + VintCurrentRow + coloumcount;
chk.Checked = true;
chk.Text = GrdShiftDetails.Rows[VintCurrentRow].Cells[coloumcount].Text;
GrdShiftDetails.Rows[VintCurrentRow].Cells[coloumcount].Controls.Add(chk);
// GrdShiftDetails.Rows[grdRow].Cells[coloumcount]
}
else {
CheckBox chk = new CheckBox();
chk.ID = "chk" + VintCurrentRow + coloumcount;
chk.Checked = false;
chk.Text = GrdShiftDetails.Rows[VintCurrentRow].Cells[coloumcount].Text;
GrdShiftDetails.Rows[VintCurrentRow].Cells[coloumcount].Controls.Add(chk);
}
}
}
答案 1 :(得分:0)
试试这个:
foreach (GridViewRow objRow in GrdShiftDetails.Rows)
{
TableCell cCheckCell = new TableCell();
CheckBox chkCheckBox = new CheckBox();
cCheckCell.Controls.Add(chkCheckBox);
objRow.Cells.Add(cCheckCell);
}
答案 2 :(得分:0)
如果您要查找已添加的CheckBox: 试试这个:
//假设vStrchkboxId是CheckBox所需的ID
for (int i = 0; i < GrdShiftDetails.Rows.Count; i++)
if(((CheckBox)GrdShiftDetails.FindControl(Convert.ToString(vStrchkboxId))).Checked)
//do something here.