how to retrieve multiple selected value from asp:checkbox .net c#
31,262
Solution 1
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.
Solution 2
Propably the simplest approach is this:
foreach (ListItem item in myCheckboxList.Items)
{
if (item.Selected)
{
// do something with this item
}
}
Solution 3
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);
Solution 4
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 />";
}
}
Solution 5
foreach (ListItem item in myCheckboxList.Items)
{
if (item.Selected)
{
//Your code goes here
}
}
Author by
Jin Yong
Updated on July 16, 2022Comments
-
Jin Yong almost 2 years
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>