Home > ASP.Net > Create Comma Separated list of CheckBox Values

Create Comma Separated list of CheckBox Values


Hi all,

try this example to Create Comma Separated list of CheckBox Values

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem Text="Text1" Value="1"></asp:ListItem>
<asp:ListItem Text="Text1" Value="2"></asp:ListItem>
<asp:ListItem Text="Text1" Value="3"></asp:ListItem>
<asp:ListItem Text="Text1" Value="4"></asp:ListItem>
<asp:ListItem Text="Text1" Value="5"></asp:ListItem>
<asp:ListItem Text="Text1" Value="6"></asp:ListItem>
</asp:CheckBoxList>


<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />


<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>

In Code behind :


using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestProject
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
Collection<string> items = new Collection<string>();
for (int index = 0; index < CheckBoxList1.Items.Count; index++)
{
if (CheckBoxList1.Items[index].Selected)
{
items.Add(CheckBoxList1.Items[index].Value);
}
}
//here you will get selected values
Label1.Text = String.Join(",", items.ToArray());
}
}
}

Hope this helps

Good Luck.

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: