[問題] 抓不到GridView裡的checkbox?
我參考聖殿祭司的.net3.5專家技術手冊I裡面的
動態建立 TemplateField 樣板欄位寫了一支程式
在Gridview裡動態生成checkbox,在Gridview的footer產生一個Button
在按下Button引發的 RowCommand 事件裡抓出已選取的checkbox
現在的問題是RowCommand假如寫成
CheckBox chk;
foreach (GridViewRow rowItem in GridView1.Rows)
{
if (rowItem.RowType == DataControlRowType.DataRow)
{
chk = (CheckBox)(rowItem.Cells[0].FindControl("cbox"));
if (chk.Checked)
{
}
}
}
就會引發 Object reference not set to an instance of an object. Error
如果寫成
CheckBox chk;
foreach (GridViewRow rowItem in GridView1.Rows)
{
if (rowItem.RowType == DataControlRowType.DataRow)
{
chk = (CheckBox)(rowItem.Cells[0].Controls[0]);
}
}
就會引發Specified argument was out of the range of valid values.
Parameter name: index
看起來像是沒抓到CheckBox,難不成CheckBox根本不在那個NamingContainer裡@@
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.165.170.34
→
09/29 22:53, , 1F
09/29 22:53, 1F
→
09/29 23:27, , 2F
09/29 23:27, 2F
推
09/30 00:08, , 3F
09/30 00:08, 3F
→
09/30 00:09, , 4F
09/30 00:09, 4F
→
09/30 00:11, , 5F
09/30 00:11, 5F
→
10/02 14:22, , 6F
10/02 14:22, 6F
→
10/02 14:22, , 7F
10/02 14:22, 7F