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

Posted on 2005-05-03
Last Modified: 2010-04-07
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.RepeatLayout = RepeatLayout.Flow;
                        //checkEquipmentType.RepeatColumns = 2;
                        phEquipmentType.Controls.Add(new LiteralControl("  "));
                        TextBox txtEquipmentTypeOther = new TextBox();
                        txtEquipmentTypeOther.ID = "txtEquipmentTypeOther";
                        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?
Question by:IUAATech
    LVL 15

    Accepted Solution

    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...

    Author Comment

    awesome. thank you!
    LVL 15

    Expert Comment

    :) u r welcome.

    Good Luck

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now