C # CheckBox List Selected Items.Text for Labels.Text

I have a CheckBoxList and 5 shortcuts.

I would like the text value of these labels to be set to 5 selected from CheckBoxList after the user clicks the button. How can I achieve this?

Thanks in advance.

+2
source share
4 answers
  • bind an event to a button,
  • iterate through the Items property CheckBoxList
  • set the text value according to the selected listitem property

as:

 protected void button_Click(object sender, EventArgs e) { foreach (ListItem item in theCheckBoxList.Items) { item.Text = item.Selected ? "Checked" : "UnChecked"; } } 

to add a value you could do:

  foreach (ListItem item in theCheckBoxList.Items) { item.Text = item.Selected ? item.Value : ""; } 

or display al values ​​in the mini-report:

  string test = "you've selected :"; foreach (ListItem item in theCheckBoxList.Items) { test += item.Selected ? item.Value + ", " : ""; } labelResult.Text = test; 
+3
source

find selected items from CheckboxList by Lambda Linq:

 var x = chkList.Items.Cast<ListItem>().Where(i => i.Selected); if (x!=null && x.Count()>0) { List<ListItem> lstSelectedItems = x.ToList(); //... Other ... } 
+1
source

Why you don’t have one label and click on the button to do something like:

 foreach (var li in CheckList1.Items) { if(li.Checked) Label1.Text = li.Value + "<br />"; } 

This may not be the exact syntax, but something like that.

0
source

Use this in LINQ:

 foreach (var cbx3 in CheckBoxList2.Controls.OfType<CheckBox>().Where(cbx3 => cbx3.ID == s)) { cbx3.Checked = true; } 
0
source

All Articles