List<string> myControlList;
CheckBoxList SubCategoryChkboxLst = new CheckBoxList();
the above two are declared as global in load.
this is for saving in to viewstate:
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
myControlList = (List<string>)ViewState["myControlList"];
foreach (string ctlID in myControlList)
{
CheckBoxList cklt = new CheckBoxList();
cklt.ID = ctlID;
LiteralControl lineBreak = new LiteralControl("<br />");
SubCategoryPnl.Controls.Add(cklt);
SubCategoryPnl.Controls.Add(lineBreak);
}
}
CategoryChkboxLst
is the above "CategoryChkboxLst" control I drag droped into my page on selected change event of categoryChlboxlst I am trying to generate dynamic checkbox list which is undertcategory.
protected void CategoryChkboxLst_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < CategoryChkboxLst.Items.Count; i++)
{
if (CategoryChkboxLst.Items[i].Selected == true)
{
objdc.ItemID = Convert.ToInt32(CategoryChkboxLst.Items[i].Value);
SubCategoryChkboxLst.ID = "SubCategoryChkboxLst_" + objdc.ItemID;
if (SubCategoryPnl.FindControl(SubCategoryChkboxLst.ID) != null)
{
this.ViewState.Remove(SubCategoryChkboxLst.ID);
SubCategoryPnl.Controls.Remove(SubCategoryPnl.FindControl(SubCategoryChkboxLst.ID));
myControlList.Remove(SubCategoryChkboxLst.ID);
}
DataTable dt = new DataTable();
dt = objbl.GetSubCategories(ref objdc);
SubCategoryChkboxLst.DataSource = dt;
SubCategoryChkboxLst.ID = "SubCategoryChkboxLst_" + objdc.ItemID;
SubCategoryChkboxLst.DataTextField = "SUB_ITEM_NAME";
SubCategoryChkboxLst.DataValueField = "SUB_ITEM_ID";
SubCategoryChkboxLst.DataBind();
SubCategoryChkboxLst.RepeatColumns = 5;
SubCategoryChkboxLst.RepeatDirection = RepeatDirection.Horizontal;
SubCategoryPnl.Controls.Add(SubCategoryChkboxLst);
SubCategoryPnl.Controls.Add(new LiteralControl("<br>"));
myControlList.Add(SubCategoryChkboxLst.ID);
ViewState["myControlList"] = myControlList;
}
}
}
I am not able to find errors
1) same control with same name. I cleared everying but still it coming.
2) one selected change event generating one checkboxlist on another selected change event it generating but first one is getting clear.