How to get multiple selected values ​​from asp: checkbox.net C #

Does anyone know how I can get a multiple select value from asp: checkbox.net C #?

Example: I am new to .net C #, I have the following code, but I have no idea how to get 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>  
+5
source share
6 answers

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 the CheckBoxList class .

+5
source

Simply put, the easiest way:

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

listitem.Selected ,

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 />";
    }
}
+1
foreach (ListItem item in myCheckboxList.Items)
{
  if (item.Selected)
  {
    //Your code goes here
  }
}
+1

, .NET 4.5 ( , ), LINQ :

IEnumerable<ListItem> selectedItems = myCheckboxList.Items.Cast<ListItem>().Where(x => x.Selected);
+1
source

You have to iterate through Items.

Refer to the CheckBoxList class .

To determine which items are checked, iterate over the collection and test the property of Selectedeach item in the list.

0
source

All Articles