Reputation: 43788
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>
Upvotes: 5
Views: 32817
Reputation: 189
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);
Upvotes: 2
Reputation: 6974
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.
Upvotes: 0
Reputation: 4081
foreach (ListItem item in myCheckboxList.Items)
{
if (item.Selected)
{
//Your code goes here
}
}
Upvotes: 1
Reputation: 26942
Propably the simplest approach is this:
foreach (ListItem item in myCheckboxList.Items)
{
if (item.Selected)
{
// do something with this item
}
}
Upvotes: 4
Reputation: 14781
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.
Upvotes: 6
Reputation: 176956
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 />";
}
}
Upvotes: 1