5

Does anyone know how can I retrieved the multiple selected value from asp:checkbox .net c#?

Example: I'm new in .net c#, I have the following code, but I have no idea how can I retrieved the multiple selected value from .net c#

<tr>   
    <th class="graytext r">Add Test:</th>
    <td>
        <asp:CheckBoxList ID="Test" runat="server" DataSourceID="dsTest" CssClass=""
            DataValueField="employeeid" DataTextField="fullname" 
            AppendDataBoundItems="false" >
            <asp:ListItem></asp:ListItem>
        </asp:CheckBoxList>  
        <asp:SqlDataSource ID="dsTest" runat="server" 
            ConnectionString="<%$ ConnectionStrings:SmartStaffConnectionString %>"
            SelectCommand="app_dsTest_select" SelectCommandType="StoredProcedure">
        </asp:SqlDataSource>
    </td>
</tr>  
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
Jin Yong
  • 42,698
  • 72
  • 141
  • 187

6 Answers6

6

Use the following:

for (int i=0; i<checkboxlist1.Items.Count; i++)
{
    if (checkboxlist1.Items[i].Selected)
    {
        Message.Text += checkboxlist1.Items[i].Text + "<br />";
    }
}

Refer to CheckBoxList Class.

Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
  • 2
    Let's go functional: var rs = checkboxlist1.Items.Cast().Where(i => i.Selected).Select(t=>t.Text).ToList(); and then you can concatenate http://stackoverflow.com/questions/559415/concat-all-strings-inside-a-liststring-using-linq – eloycm Jun 14 '13 at 14:40
4

Propably the simplest approach is this:

foreach (ListItem item in myCheckboxList.Items)
{
  if (item.Selected)
  {
    // do something with this item
  }
}
Bazzz
  • 26,427
  • 12
  • 52
  • 69
2

This is an old thread, but using .NET 4.5 (not sure if previous versions work), you can use LINQ to do this:

IEnumerable<ListItem> selectedItems = myCheckboxList.Items.Cast<ListItem>().Where(x => x.Selected);
D. Dubya
  • 189
  • 1
  • 12
1

try listitem.Selected property as i did below

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
    Label1.Text = string.Empty;

    foreach (ListItem listitem in CheckBoxList1.Items)
    {
        if (listitem.Selected)
            Label1.Text += listitem.Text + "<br />";
    }
}
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
1
foreach (ListItem item in myCheckboxList.Items)
{
  if (item.Selected)
  {
    //Your code goes here
  }
}
Bibhu
  • 4,053
  • 4
  • 33
  • 63
  • CheckboxList doesnt have a property `SelectedItems`, there is a property named `SelectedItem` (singular) but that just returns the first selected item. – Bazzz Jun 27 '11 at 07:46
  • @Bazz - But ListBox has so also CheckListBox. Check out this link, http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.aspx – Bibhu Jun 27 '11 at 07:53
  • sorry to disappoint you but your reference is for winforms, OP is using asp.net http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist.aspx – Bazzz Jun 27 '11 at 19:58
0

You must iterate through the Items.

Refer to CheckBoxList Class.

To determine which items are checked, iterate through the collection and test the Selected property of each item in the list.

Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
Justin M. Keyes
  • 6,679
  • 3
  • 33
  • 60