Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The below code is used to create a dynamic checkbox list:
C#
CheckBoxList SubCategoryChkboxLst;
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);
      DataTable dt = new DataTable();
      dt = objbl.GetSubCategories(ref objdc);
                 
      SubCategoryChkboxLst = new CheckBoxList();
      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;

      //adding to panel for appearing in specified location
      SubCategoryPnl.Controls.Add(SubCategoryChkboxLst);
      SubCategoryPnl.Controls.Add(new LiteralControl("<br>"));
    }
  }
}

I have a problem in getting value & finding control in save button.When I find the control, I am getting null
C#
protected void SaveImgbtn_Click(object sender, ImageClickEventArgs e)
{
  try
  {
    if (ProductNameCmbBox.SelectedIndex > 0)
    {             
      //karthik written  code
      for (int i = 0; i < CategoryChkboxLst.Items.Count; i++)
      {
        if (CategoryChkboxLst.Items[i].Selected == true)
        {
          objdc.ItemID = Convert.ToInt32(CategoryChkboxLst.Items[i].Value);
          string stId="SubCategoryChkboxLst_"+objdc.ItemID;
          CheckBoxList cklt = (CheckBoxList)Page.FindControl(stId);
          //here I am getting null ie cklt is null

          foreach (ListItem item in cklt.Items)
          {
            if (item.Selected)
            {
              objdc.subcategory += item.Value;
              //string Activities += item.Value;
            }
          }
        }
      }
      // code end

      objdc.insertedby = Session["UserId"].ToString();
      int result = objbl.InsertProductscheme(ref objdc);
      if (result == 1)
      {
        GridBindMethod();
        Literal li = new Literal();
        li.Text = "<script>alert('Data Inserted Sucessfully');</script>";
        Page.Controls.Add(li);
        Clearfields();
      }
      else
      {
        Literal li = new Literal();
        li.Text = "<script>alert('Insertion Failed');</script>";
        Page.Controls.Add(li);
      }
    }
  }
  catch (Exception ex)
  {
    log.Error(ex.Message, ex);
  }
}
Posted
Updated 25-Jan-11 20:26pm
v2
Comments
JF2015 26-Jan-11 2:26am    
Improved code formatting.

1 solution

you should read this links
Click
Click
Click
 
Share this answer
 
Comments
karthikkushala 28-Jan-11 7:41am    
in any event checkboxlist is getting clear

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