Sep 02 2009

asp.net checkboxlist selected value

step1: Drag CheckBoxList and Button controls

<asp:CheckBoxList ID=”CheckBoxList1″ runat=”server” RepeatColumns=”2″>
<asp:ListItem>Apples</asp:ListItem>
<asp:ListItem>Grapes</asp:ListItem>
<asp:ListItem>Bananas</asp:ListItem>
<asp:ListItem>Mangos</asp:ListItem>
<asp:ListItem>Oranges</asp:ListItem>
<asp:ListItem>Oranges</asp:ListItem>
<asp:ListItem>Cocoa</asp:ListItem>
<asp:ListItem>CustardApple</asp:ListItem>
<asp:ListItem>Breadfruit</asp:ListItem>
</asp:CheckBoxList>
<br />
<asp:Button ID=”Button2″ runat=”server” onclick=”Button2_Click” Text=”Button” />

Step2: use ArrayList to get CheckBoxList Selected values into Label control

Note: Remove Label Text Property (keep label clean without any text) i.e

By Default Label Text property = Label(Remove this text) in Label Property

if not checkboxlist items display as

LabelMovies,Furniture,Electronics (so remove label text)

  • call namespace System.Collections

Sample code

//Getting CheckBoxlist selected values into Label

using System.Collections;

protected void Button2_Click(object sender, EventArgs e)
{

ArrayList chkItems = new ArrayList();
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected)
{
chkItems.Add(li.Text);
string selItems = li.Text+”,”;
Label1.Text += selItems;

}
}

//Remove last character from string
Label1.Text= Label1.Text.Remove(Label1.Text.Length – 1,1);

//Getting CheckBoxlist items into Gridview

//Drag Gridview control write below code

GridView1.DataSource = chkItems;
GridView1.DataBind();
}

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

Leave a Reply

Alibi3col theme by Themocracy