In this article I will show you how you can get the selected value of your checkboxlist in your asp.net application using c#.net. in this I will use LINQ and for look to get the selected checkboxlist value your can get.
So first we will take a checkboxlist control and bind some value.
<asp:CheckBoxList ID="cblItemType" runat="server" RepeatDirection="Horizontal"> <asp:ListItem>Item1</asp:ListItem> <asp:ListItem>Item2</asp:ListItem> <asp:ListItem>Item3</asp:ListItem> </asp:CheckBoxList> |
Now here are the code:
With LINQ to get selected item value:
List<string> selectedItem = cblItemType.Items.Cast<ListItem>() .Where(li => li.Selected) .Select(li => li.Value) .ToList(); |
With Foreach:
List<string> selectedItem = new List<string>(); foreach(ListItem item in cblItemType.Items) { if (item.Selected) { selectedItem.Add(item.Value); } } |
Now run the article to check the output.
0 comments:
Please let me know your view