Click here to Skip to main content
16,019,152 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
C#
List<string> myControlList;
CheckBoxList SubCategoryChkboxLst = new CheckBoxList();

the above two are declared as global in load.
this is for saving in to viewstate:
C#
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);
  }
}

C#
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.
C#
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;
      //lblYear.ID = "lbl_" + li.Value;

      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.
Posted
Updated 30-Jan-11 18:25pm
v2
Comments
karthikkushala 31-Jan-11 23:41pm    
please any body can help
dontumindit 15-Feb-11 15:33pm    
wt u r trting to do, i cudnt get wt u want, can u explain a bit wts ur error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900