How to return selected items from checkboxlist control in asp.net

asp.net, c#

Solution

here is complete code of page working exactly as you want it. just add a CheckboxList to form name it to list1, add a button name it to btn and add a label and name it lbl.

protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                var dt = new DataTable();
                dt.Columns.Add("Users");

                const string str = "User {0}";
                for(var i=1;i<=10;i++)
                {
                    //var r = dt.NewRow();
                    //r.ItemArray=new object[]{string.Format(str,i)};
                    dt.Rows.Add(new object[] {string.Format(str, i)});
                }
                list1.DataSource = dt;
                list1.DataTextField = "Users";
                list1.DataBind();
            }
        }

        protected void btn_Click(object sender, EventArgs e)
        {
            var s = list1.Items.Cast<ListItem>()
                   .Where(item => item.Selected)
                   .Aggregate("", (current, item) => current + (item.Text + ", "));
            lbl.Text = s.TrimEnd(new[] {',', ' '});
        }

Problem

I am trying to return in a string the selected items from a dynamically bound checkbox list control with no luck. I'm hoping someone can help. In my code behind file I am conencting to a class called users and building a datatable. I then bind the data table to the cblist control ``` private void populateUserList() //called on page load { SubmitOptions mySubmission = new SubmitOptions(juris, rptType, tmplName); if (mySubmission.Users.Count == 0) { lbl_juris.Visible = false; cb_selectUser.Visible = false; lbl_AlertMsg.Visible = true; btnSelect.Visible = false; lbl_AlertMsg.Text = "No supervisors listed for jursidiction: " + juris.ToString(); } else { dt.Columns.Add("Users"); for (int i = 0; i < mySubmission.Users.Count(); i++) { DataRow dr = dt.NewRow(); dr["Users"] = mySubmission.Users[i]; dt.Rows.Add(dr); } cb_selectUser.DataSource = dt; cb_selectUser.DataBind(); } } ``` Within the main aspx file I have the control defined as: ``` <asp:CheckBoxList ID="cb_selectUser" Width="400px" Height="100%" AutoPostBack="false" runat="server" CellPadding="2" CellSpacing="5" DataTextField="Users" DataValueField="Users" > </asp:CheckBoxList> ``` I have tried the following code where i itterate through the list but this only seems to work if I hard code values into the Checkboxt list as listitems. ``` protected void btn_returnUserList(object sender, System.Web.UI.ImageClickEventArgs e) { for (int i = 0; i < cb_selectUser.Items.Count; i++) { if (cb_selectUser.Items[i].Selected) { selectedUsers += cb_selectUser.Items[i].Text; } } ``` The list populates fine and all I want to do is return in a string all selected users from the checkbox list control. As I said if I hard code the item values into the control the above code works and I can see the selected items in the string however removing the itemslist tags and switching over to a binding nothign happens. The above method counts the entire number of returns but nothing selected is ever returned. Any tips or suggestions as to what I am missing would be greatly appreciated.

Original source