Friday 7 February 2014

How to get selected text of CheckBoxList and align CheckBoxList Horizontal in ASP.NET using C#/VB.NET

Hi Friends,in this article i will explain about How to get selected text of CheckBoxList and align CheckBoxList Horizontal in ASP.NET using C#/VB.NET.
ASP.NET:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to get selected text of CheckBoxList and align CheckBoxList Horizontal in ASP.NET using C#/VB.NET</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td align="left"  width="15%">
                   Courses
                </td>
                <td width="2%">
                    :
                </td>
                <td align="left">
                    <asp:CheckBoxList ID="cbxCourses" runat="server" RepeatDirection="Horizontal"
                        RepeatColumns="4">
                        <asp:ListItem Text=".NET" Value="1"></asp:ListItem>
                        <asp:ListItem Text="JAVA" Value="2"></asp:ListItem>
                        <asp:ListItem Text="PHP" Value="3"></asp:ListItem>
                        <asp:ListItem Text="Android" Value="4"></asp:ListItem>
                        <asp:ListItem Text="SilverLight " Value="5"></asp:ListItem>
                        <asp:ListItem Text="Oracle" Value="6"></asp:ListItem>
                        <asp:ListItem Text="TeraData" Value="7"></asp:ListItem>
                        <asp:ListItem Text="Testing" Value="8"></asp:ListItem>
                    </asp:CheckBoxList>
                </td>
            </tr>
        </table>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit"
            onclick="btnSubmit_Click" />
    </div>
    </form>
</body>
</html>

C#:NET:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class checkboxlist : System.Web.UI.Page
{
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string selectedValue = "";
        foreach (ListItem item in cbxCourses.Items)
        {
            if (item.Selected)
            {
                selectedValue +="," + item.Text;
            }
        }
        Response.Write(selectedValue.TrimStart(','));
    }
}

In VB.NET:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Partial Public Class checkboxlist
    Inherits System.Web.UI.Page
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim selectedValue As String = ""
        For Each item As ListItem In cbxCourses.Items
            If item.Selected Then
                selectedValue += "," & item.Text
            End If
        Next
        Response.Write(selectedValue.TrimStart(","c))
    End Sub
End Class


The output of the above page as shown in the below figure.

You can download the code by clicking on the below Download image. 

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.