Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Tuesday 17 February 2015

Now in this article I will show the selected items/values from the CheckBoxList in comma-separated string.In the design file(.aspx)  place a CheckBoxList and place two buttoncontrols and a label control as given below:





   <fieldset style="width:240px">
    <legend>Show Selected Items of CheckBoxList</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 the following namespace:
using System.Collections;

write the following code :
    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 Show CheckBoxList selected items as A string (comma separated format)

Source Code:
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) HandlesBtnGetSelectedValues.Click
        If cblCourses.SelectedIndex <> -1 Then
            lblSelectedValues.Text = "Selected values are = " & GetCheckBoxListSelections()
        Else
            lblSelectedValues.Text = "Please select 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) HandlesbtnClearSelection.Click
        cblCourses.ClearSelection()
        lblSelectedValues.Text = String.Empty

    End Sub
Categories: ,

0 comments:

Post a Comment