Google How to get CheckBoxList selected items in comma separated format in asp.net(C#,VB) | Asp.Net,C#.Net,VB.Net,MVC,jQuery,JavaScipt,AJAX,WCF,Sql Server example

How to get CheckBoxList selected items in comma separated format in asp.net(C#,VB)


get checkboxlist selected items in comma separatedformat in asp.net
Click on image to enlarge
Introduction: In previous articles I explained How to Fill CheckBoxList from Sql Server table in asp.net(C#,VB) and Fill CheckBoxList based on DropDownList selection in asp.net and Bind RadioButtonList from Sql server table in asp.net and Fill DropDownList from Sql server database and Bind ListBox with Sql Server Database and  How to validate CheckBoxList using JavaScript in Asp.net

Now in this article I am going to explain how to get/read/fetch the selected items/values from the CheckBoxList in comma-separated format. This is very common requirement while working on asp.net web application. 

C#.Net Code to get CheckBoxList selected items in comma separated format

Source Code:
  • In the design file(.aspx)  place a CheckBoxList and rename it as cblCourses. Also Place two button controls and a label control as shown below:
   <fieldset style="width:240px">
    <legend>CheckBoxList Example</legend>
     <asp:CheckBoxList ID="cblCourses" runat="server" RepeatColumns="3"
            RepeatDirection="Horizontal" Width="240px">
            <asp:ListItem>Asp.net</asp:ListItem>
            <asp:ListItem>C#</asp:ListItem>
            <asp:ListItem>VB</asp:ListItem>
            <asp:ListItem>WCF</asp:ListItem>
            <asp:ListItem>LINQ</asp:ListItem>
            <asp:ListItem>MVC</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>
  •  Now in the code behind file(.aspx.cs) write the code as:
Include following namespace:
using System.Collections;

and write the code as:

    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";
        }
    }

    private string GetCheckBoxListSelections()
    {
        string[] cblItems;       
        ArrayList cblSelections = new ArrayList();
        foreach (ListItem item in cblCourses.Items)
        {
            if (item.Selected)
            {
                cblSelections.Add(item.Text);
            }
        }

        cblItems = (string[])cblSelections.ToArray(typeof(string));
        return string.Join(",", cblItems);
    }

    protected void btnClearSelection_Click(object sender, EventArgs e)
    {
        cblCourses.ClearSelection();
        lblSelectedValues.Text = string.Empty;
    }

VB.Net Code to get CheckBoxList selected items in comma separated format

Source Code:

  • In the design file(.aspx)  place a CheckBoxList and rename it as cblCourses. Also Place two button controls and a label control as shown below:
  <fieldset style="width:240px">
    <legend>CheckBoxList Example</legend>
     <asp:CheckBoxList ID="cblCourses" runat="server" RepeatColumns="3"
            RepeatDirection="Horizontal" Width="240px">
            <asp:ListItem>Asp.net</asp:ListItem>
            <asp:ListItem>C#</asp:ListItem>
            <asp:ListItem>VB</asp:ListItem>
            <asp:ListItem>WCF</asp:ListItem>
            <asp:ListItem>LINQ</asp:ListItem>
            <asp:ListItem>MVC</asp:ListItem>
        </asp:CheckBoxList>      
    </fieldset>
    <br />
        <asp:Button ID="BtnGetSelectedValues" runat="server" Text="Get Selected Values"/>
   
        <asp:Button ID="btnClearSelection" runat="server" Text="Clear Selection"/>
   
        <br />
             <br />
        <asp:Label ID="lblSelectedValues" runat="server" Text="" style="color: #FF3300"></asp:Label>
 
  • Now in the code behind file(.aspx.vb) write the code as:
First Import following namespace:

imports System.Collections

and write the code as:

Protected Sub BtnGetSelectedValues_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnGetSelectedValues.Click
        If cblCourses.SelectedIndex <> -1 Then
            lblSelectedValues.Text = "Selected values are = " & GetCheckBoxListSelections()
        Else
            lblSelectedValues.Text = "Please select any course"
        End If
    End Sub

    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)
            End If
        Next

        cblItems = DirectCast(cblSelections.ToArray(GetType(String)), String())
        Return String.Join(",", cblItems)
    End Function

    Protected Sub btnClearSelection_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClearSelection.Click
        cblCourses.ClearSelection()
        lblSelectedValues.Text = String.Empty
    End Sub

Now over to you: 
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned for more technical updates." 

4 comments :

  1. AnonymousJuly 26, 2013

    thanks!

    ReplyDelete
  2. Replies
    1. your welcome DanielFrank..keep reading for more useful updates like this..:)

      Delete
  3. AnonymousMay 07, 2014

    Do 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.

    ReplyDelete

If 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..