我正在尝试实现一个检索已经检查过的记录的函数,然后函数将它们添加到ArrayList中,然后将ArrayList保存到ViewState。基本上,有一个按钮可以从表中删除所选的行(通过复选框)。所以在Page_Load事件之后,我单击按钮删除所选行,但我得到一个nullreference异常:
An exception of type 'System.NullReferenceException' occurred
but was not handled in user code
Additional information: Object reference not set to an instance of an object.
这是功能:
protected void GetSelectedRecords()
{
ArrayList arr;
if (ViewState["SelectedRecords"] != null)
arr = (ArrayList)ViewState["SelectedRecords"];
else
arr = new ArrayList();
CheckBox chkAll = new CheckBox();
chkAll = (CheckBox)grdUnsubscribe.FindControl("chkAll");
for (int i = 0; i < grdUnsubscribe.Rows.Count; i++)
{
if (chkAll.Checked == true)
{
if (!arr.Contains(grdUnsubscribe.DataKeys[i].Value))
{
arr.Add(grdUnsubscribe.DataKeys[i].Value);
}
}
else
{
CheckBox chk = (CheckBox)grdUnsubscribe.Rows[i].Cells[0].FindControl("chk");
if (chk.Checked)
{
if (!arr.Contains(grdUnsubscribe.DataKeys[i].Value))
{
arr.Add(grdUnsubscribe.DataKeys[i].Value);
}
}
else
{
if (arr.Contains(grdUnsubscribe.DataKeys[i].Value))
{
arr.Remove(grdUnsubscribe.DataKeys[i].Value);
}
}
}
}
ViewState["SelectedRecords"] = arr;
}
提前致谢
答案 0 :(得分:0)
如果没有更多的异常细节,很难说哪个变量为null,一个好的起点就是查看引发异常的行。
例如,假设在此行上抛出异常:
if (ViewState["SelectedRecords"] != null)
然后问题是ViewState为null。