Introduction: In this article I am going to explain how to get
checkboxlist selected item text/value as Comma separated string using 3 different ways in Asp.Net
using both C# and VB Language.
Click on image to enlarge |
In previous articles i explained How to Populate CheckBoxList from sql server database and Move items from one checkboxlist to other in asp.net and Validate checkboxlist using jquery in asp.net and Jquery to check uncheck all items in checkboxlist on click of select all checkbox and Delete multiple records from gridview with checkbox selection
Description: While working on asp.net
project it is very common requirement to show checkboxlist selected items in
comma separated string. I have mentioned 3 different approaches to get comma
separated values(CSV) of checkboxlist selected items. You can pick any of the three. My preferred approach is the last one i.e. Using LINQ.
Implementation: Let’s create a
demo page for demonstration purpose.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset
style="width:
240px">
<legend>CheckBoxList Example</legend>
<asp:CheckBoxList ID="cblCourses" runat="server" RepeatColumns="3"
RepeatDirection="Horizontal" Width="240px">
<asp:ListItem Text="Asp.net" Value="1"></asp:ListItem>
<asp:ListItem Text="C#" Value="2"></asp:ListItem>
<asp:ListItem Text="VB" Value="3"></asp:ListItem>
<asp:ListItem Text="WCF" Value="4"></asp:ListItem>
<asp:ListItem Text="LINQ" Value="5"></asp:ListItem>
<asp:ListItem Text="MVC" Value="6"></asp:ListItem>
</asp:CheckBoxList>
</fieldset>
<br />
<asp:Button
ID="BtnGetSelectedValues" runat="server" Text="Get Selected Values"
OnClick="BtnGetSelectedValues_Click" />
<asp:Button
ID="btnClearSelection" runat="server" Text="Clear Selection"
OnClick="btnClearSelection_Click" />
<br />
<br />
<asp:Label
ID="lblSelectedValues" runat="server" Text="" Style="color: #FF3300"></asp:Label>
</div>
</form>
</body>
</html>
Asp.Net C# Code to get
CheckBoxList selected items in comma separated format
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
protected void BtnGetSelectedValues_Click(object
sender, EventArgs e)
{
if (cblCourses.SelectedIndex != -1)
{
lblSelectedValues.Text = "Selected values are = " + GetCheckBoxListSelections();
}
else
{
lblSelectedValues.Text = "Please select any course";
}
}
//
Get selected items Using ArrayList and String.Join method
private string GetCheckBoxListSelections()
{
string[] cblItems;
ArrayList cblSelections = new ArrayList();
foreach (ListItem item in cblCourses.Items)
{
if (item.Selected)
{
cblSelections.Add(item.Text);
//if
you want values instead of text then cblSelections.Add(item.Value);
}
}
cblItems = (string[])cblSelections.ToArray(typeof(string));
return string.Join(", ", cblItems);
}
//
Get selected items Using List and String.Join method
private string GetCheckBoxListSelections()
{
List<string> items = new List<string>();
for (int i = 0; i < cblCourses.Items.Count;
i++)
{
if (cblCourses.Items[i].Selected)
{
items.Add(cblCourses.Items[i].Text);
//if you want values instead of text then items.Add(cblCourses.Items[i].Value);
}
}
return String.Join(", ", items.ToArray());
}
private string GetCheckBoxListSelections()
{
return string.Join(", ",
cblCourses.Items.Cast<ListItem>().Where(li =>
li.Selected).Select(x => x.Text).ToArray());
//if
you want values instead of text then return string.Join(", ",
cblCourses.Items.Cast<ListItem>().Where(li => li.Selected).Select(x
=> x.Value).ToArray());
}
protected void btnClearSelection_Click(object
sender, EventArgs e)
{
cblCourses.ClearSelection();
lblSelectedValues.Text = string.Empty;
}
Asp.Net VB Code to get
CheckBoxList selected items in comma separated format
Protected Sub BtnGetSelectedValues_Click(sender As Object,
e As EventArgs)
Handles
BtnGetSelectedValues.Click
If cblCourses.SelectedIndex <> -1 Then
lblSelectedValues.Text = Convert.ToString("Selected values are = ") & GetCheckBoxListSelections()
Else
lblSelectedValues.Text = "Please select any course"
End If
End Sub
'Get selected items Using ArrayList and String.Join method
Private Function GetCheckBoxListSelections() As String
Dim cblItems As String()
Dim cblSelections As New ArrayList()
For Each item As ListItem In cblCourses.Items
If item.Selected Then
cblSelections.Add(item.Text)
'if
you want values instead of text then cblSelections.Add(item.Value)
End If
Next
cblItems = DirectCast(cblSelections.ToArray(GetType(String)),
String())
Return String.Join(", ", cblItems)
End Function
Private Function GetCheckBoxListSelections() As String
Dim items As New List(Of String)()
For i As Integer = 0 To cblCourses.Items.Count - 1
If cblCourses.Items(i).Selected Then
items.Add(cblCourses.Items(i).Text)
'if
you want values instead of text then items.Add(cblCourses.Items(i).Value)
End If
Next
Return [String].Join(", ", items.ToArray())
End Function
Private Function GetCheckBoxListSelections() As String
Return String.Join(", ",
cblCourses.Items.Cast(Of ListItem)().Where(Function(li) li.Selected).[Select](Function(x)
x.Text).ToArray())
'If
you want values instead of text then Return String.Join(", ",
cblCourses.Items.Cast(Of ListItem)().Where(Function(li)
li.Selected).[Select](Function(x) x.Value).ToArray())
End Function
Protected Sub btnClearSelection_Click(sender As Object,
e As EventArgs)
Handles
btnClearSelection.Click
cblCourses.ClearSelection()
lblSelectedValues.Text = String.Empty
End Sub
Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates.
4 comments
Click here for commentsthanks!
ReplyThanks!
Replyyour welcome DanielFrank..keep reading for more useful updates like this..:)
ReplyDo you have an example that let a user search a database according to which checkbox list is selected? Also using a textbox for search value.
ReplyIf you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..