Click here to Skip to main content
15,884,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sir am Generating Dynamic Checkboxlist on selected change even of main Checkboxlist.
the below code........

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 = new CheckBoxList();
                    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 added to view state to save controls

XML
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);
    }


}


but on any dropdown change event or button click event generated list box is getting clear .wht is the proble please can any one help.

and i how how can i get control id in Save button
Posted
Comments
Abdul Rahman Hamidy 28-Jan-11 12:00pm    
I had the same issue generating dynamic controls, I used PageLoad to build up my dynamic controls.
I am asking generate your Controls in PageLoad in PostBack, as I see the CheckBox do Postback.

1 solution

You are Creating each time the new object of checklist in CategoryChkboxLst_SelectedIndexChanged method.by using new keyword so new memory is allocated and it becomes blank.
you do not have to create it each time.
you create it in load and then use it in CategoryChkboxLst_SelectedIndexChanged method.
 
Share this answer
 
Comments
karthikkushala 30-Jan-11 23:55pm    
CheckBoxList SubCategoryChkboxLst = new CheckBoxList(); i created object in load . am geeting error same id control, and when i click first checkbox of parent it generating checkboxlist when i check 2nd check box list it generating checkboxlist but first generated is get clearing

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