【发布时间】:2010-03-24 01:23:56
【问题描述】:
我有一个 web 表单,它将根据数据库中的行数动态创建一些填充数据的复选框列表。然后将其添加到要显示的占位符中。
点击时有一个按钮,会将选中的复选框值添加到数据库中,但现在当点击按钮时和回发后,页面将显示错误“System.NullReferenceException”。
下面的代码是写在Page_Load中的(!Page.IsNotPostBack)中,并在一个循环中会动态创建多个checkboxlist:
CheckBoxLis chkContent = new CheckBoxList();
chkContent.ID = chkIDString; //chkIDString is an incremental int based on the row count
chkContent.RepeatDirection = RepeatDirection.Horizontal;
foreach (List<t> contentList in List<t>) //data retrieved as List<t> using LINQ
{
ListItem contents = new ListItem();
contents.Text = contentList.Title;
contents.Value = contentList.contentID.ToString();
chkContent.Items.Add(contents);
}
plcSchool.Controls.Add(chkContent); //plcSchool is my placeholder
plcSchool.Controls.Add(new LiteralControl("<br>"));
protected void btnAdd_Click(object sender, EventArgs e)
{
CheckBoxList cbl = Page.FindControl("chkContent4") as CheckBoxList;
Response.Write(cbl.SelectedValue.ToString()); // now im just testing to get the value from one of the checkboxlist
}
任何人都可以提供帮助,因为似乎控件在回发后没有重新创建,因此它找不到控件并抛出空引用异常。
【问题讨论】:
标签: c# postback nullreferenceexception checkboxlist