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>  
2
  • Are you looking for selected items or checked ones ?? Commented Jun 27, 2011 at 7:09
  • @Akram Shahda This is the same for the CheckBoxList Commented Jun 28, 2011 at 5:33

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Let's go functional: var rs = checkboxlist1.Items.Cast<ListItem>().Where(i => i.Selected).Select(t=>t.Text).ToList(); and then you can concatenate stackoverflow.com/questions/559415/…
4

Propably the simplest approach is this:

foreach (ListItem item in myCheckboxList.Items)
{
  if (item.Selected)
  {
    // do something with this item
  }
}

Comments

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);

Comments

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 />";
    }
}

Comments

1
foreach (ListItem item in myCheckboxList.Items)
{
  if (item.Selected)
  {
    //Your code goes here
  }
}

3 Comments

CheckboxList doesnt have a property SelectedItems, there is a property named SelectedItem (singular) but that just returns the first selected item.
@Bazz - But ListBox has so also CheckListBox. Check out this link, msdn.microsoft.com/en-us/library/…
sorry to disappoint you but your reference is for winforms, OP is using asp.net msdn.microsoft.com/en-us/library/…
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.

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.