对象引用未设置为对象的实例.
protected void cmdSave_Click(object sender, EventArgs e)
{
string strNames = string.Empty;
CheckBoxList Chkboxx = (CheckBoxList)PlaceHolder1.FindControl("Chkbox");
foreach (ListItem em in Chkboxx.Items) //-------- (Showing error)
{
if (em.Selected)
{
strNames += em.Value + ", ";
}
}
string final_name = strNames.Substring(0, strNames.Length - 2);
lblNames.Text = final_name;
}实际上我是动态地添加复选框控件:
protected void ddl_varient_SelectedIndexChanged1(object sender, EventArgs e)
{
string query = "select prd_vrtyvalue_id,varient_value from tbl_ProductVariety_Value where varient='" + ddl_varient.SelectedItem.Text + "' " +
" order by varient_value asc ";
DataTable abc = new DataTable();
SqlDataAdapter ada = new SqlDataAdapter(query, new CommonClass().connection());
ada.Fill(abc);
ChkboxList.ID = "Chkbox";
for (int i = 0; i < abc.Rows.Count; i++)
{
ChkboxList.Items.Add(new ListItem(abc.Rows[i]["varient_value"].ToString(), abc.Rows[i]["prd_vrtyvalue_id"].ToString()));
}
ChkboxList.RepeatColumns = 2;
PlaceHolder1.Controls.Add(ChkboxList);
}任何人都能告诉我,我到底做错了什么!
发布于 2013-03-18 10:24:10
ASP.NET WebForms的工作方式是在每次回发期间重新构建整个页面。所以,我想这就是正在发生的事情:
要修复,可以根据ChkboxList复选框的值在Page_Load上重新添加DDL_VARIENT。但是,如果我是您,我会尝试在您的ASPX/ASCX代码中定义ChkboxList,然后根据DDL_VARIENT复选框在Page_Load中的值设置列表的可见性。
我要补充的是,以上所有内容都依赖于使用ASP.NET WebForms的您。如果您正在使用MVC,那么它可能是错误的。
https://stackoverflow.com/questions/15474155
复制相似问题