1

I have dynamically populated data to my checkboxlist from SqlServer data base.. using code

aspx

<asp:CheckBoxList ID="CheckBoxList1" runat="server">
</asp:CheckBoxList>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    SqlConnection cn = new SqlConnection(
        ConfigurationManager.ConnectionStrings["testingCS"].ConnectionString);
    cn.Open();
    SqlCommand cmd1 = new SqlCommand("select * from tbl_vehicletypes", cn);
    SqlDataReader dr = cmd1.ExecuteReader();
    if (dr.HasRows) {
        CheckBoxList1.DataSource = dr;
        CheckBoxList1.DataTextField = "typedesc";
        CheckBoxList1.DataValueField = "vehicletypeid";
        CheckBoxList1.DataBind();
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    string[] arr = new string[10];

    for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        if (CheckBoxList1.Items[i].Selected==true)
            arr[i] = CheckBoxList1.Items[i].Value;

    foreach (string s in arr)
        Label2.Text += s + "<br />";
    Label2.Text = CheckBoxList1.Items.Count.ToString();
}

I tried using for loop and foreach as well but it didn't work out. The selected data is not getting added to string array..

Please help

2 Answers 2

1

Try this

List<string> selectedValues = CheckBoxList1.Items.Cast<ListItem>()
   .Where(li => li.Selected)
   .Select(li => li.Value)
   .ToList();

foreach(string s in selectedValue)
   Label2.Text += s + "<br />";
Sign up to request clarification or add additional context in comments.

6 Comments

Im facing issues in the for loop when im tracing using checkpoints.. CheckBoxList1.Items[i].Selected shows false even if the item is selected... out of 8 elements i selected first 2 elements but still it doesnt add to the string array
post the entire code so we can check the implementation for you. Thanx
What is the Label2? I don't see it in your code. There is nothing wrong with the button click code I believe. So the only problem is that Label2 is not showing the text. Could you post the code for Label2?
I just placed a label control.. to display what the data which is copied into the array... at the moment when i run the code. it shows 8 elements in the checkboxlist.. if i select first 2 .. and click the button.. and start tracing it.. if (CheckBoxList1.Items[i].Selected==true) this statement is not getting validated.. for the first 2 ..
If you want to dynamically change the text as the user selects the values, use JavaScript
|
0

This worked for me, hope it helps! Just a demo with one item but it'll work for any number of items in your CheckBoxList. How are you selecting the checkboxes?

CheckBoxList cbl = new CheckBoxList();

ListItem li = new ListItem("test");
li.Selected = true;
cbl.Items.Add(li);

Label labelTest = new Label();            

string[] test = cbl.Items.Cast<ListItem>().Where(l => l.Selected)
                                          .Select(l => l.Value)
                                          .ToArray();

labelTest.Text = String.Join("<br /", test);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.