0

在这里,我需要在 asp.net Web 应用程序的动态网格中动态创建复选框,并且我使用以下代码来做理智,然后我需要使用 Find Control 检索这些 Id -((CheckBox)GrdShiftDetails.FindControl(Convert.ToString(vStrchkboxId))).Checked 但显示错误消息,如

空引用(对象引用未设置为对象的实例)

所以请帮我解决这个问题。

if (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);
        }
    }
4

3 回答 3

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);
        }
    }
}
于 2013-01-12T06:06:53.153 回答
0

尝试这个:

foreach (GridViewRow objRow in GrdShiftDetails.Rows)
{
   TableCell cCheckCell = new TableCell();
   CheckBox chkCheckBox = new CheckBox();
   cCheckCell.Controls.Add(chkCheckBox);

   objRow.Cells.Add(cCheckCell);
}
于 2013-01-12T18:05:49.920 回答
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.
于 2013-01-12T19:06:59.207 回答