[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

How to get a list of selected checkboxes from a checkboxlist that was dynamically created?

Here is the code that I have so far:

private void Page_Load(object sender, System.EventArgs e)
            {
                  if (!IsPostBack)
                  {      
                        string sqlText;
                        //create Equipment Type check box list
                        CheckBoxList checkEquipmentType = new CheckBoxList();
                        checkEquipmentType.ID = "checkEquipmentType";
                        sqlText = "select * from EquipmentType";
                        checkEquipmentType.DataSource = DBFunctions.GetDR(sqlText);
                        checkEquipmentType.DataTextField= "Equipment";
                        checkEquipmentType.DataValueField = "EquipmentTypeId";
                        checkEquipmentType.DataBind();
                        checkEquipmentType.RepeatLayout = RepeatLayout.Flow;
                        //checkEquipmentType.RepeatColumns = 2;
                        phEquipmentType.Controls.Add(checkEquipmentType);
                        phEquipmentType.Controls.Add(new LiteralControl("  "));
                        TextBox txtEquipmentTypeOther = new TextBox();
                        txtEquipmentTypeOther.ID = "txtEquipmentTypeOther";
                        phEquipmentType.Controls.Add(txtEquipmentTypeOther);
                  }
                  else
                  {
                        someLabel.Text = getSelected();
                  }
            }

private string getSelected()
            {
                  String s = "";
         
                  CheckBoxList cbList = (CheckBoxList)FindControl("checkEquipmentType");
                  for(int i = 0; i < cbList.Items.Count; i++)
                  {
                        if(cbList.Items[i].Selected )
                        {
                              s += cbList.Items[i].Text + "<BR>";
                        }
                  }
                  return s;
            }

when I try to run this code, I get a System.NullReferenceException: Object reference not set to an instance of an object. It is supposed to be occuring at for(int i = 0; i < cbList.Items.Count; i++).

can somebody tell me what I am doing wrong?
0
IUAATech
Asked:
IUAATech
  • 2
1 Solution
 
praneethaCommented:
you need to create the control at eveyr page_load since it is dynamic...

just try

removing this check

 if (!IsPostBack)
               {  

} in page load...and let it create the control everytime...
0
 
IUAATechAuthor Commented:
awesome. thank you!
0
 
praneethaCommented:
:) u r welcome.

Good Luck
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now