If you want to store selected value of a CheckBoxList or
Listbox in comma separated string, then you are on right place. Sometime it is
requirement that we want a comma separated string of selected value of
CheckBoxList or Listbox control (I.e. to save your Control’s selected value in one
String field of DB).
So here I am writing one small function which will take CheckBoxList
or ListBox as parameter and return comma separated string of selected items.
//for Listbox:provide ListBox control.
public string GetSelectedItems(CheckBoxList control)
{
var items
= new StringBuilder();
foreach (ListItem item in
control.Items)
{
if
(item.Selected)
items.Append(string.Format("{0},",
item.Value));
}
//Remove last
comma before returning back.
return
items.ToString().TrimEnd(',');
}
|
Use this function as:
var selectedValues =
GetSelectedItems(control); //control is name of Checkboxlist/ListBox
|
In my next post I will demonstrate how you can populate selected value for CheckBoxList or Listbox from comma separated string.
Thanks Sandeep.
ReplyDeleteHow to select the Checkboxlist with the DB string field
Mathew
Methew,
ReplyDeleteI think you are asking how to select checkboxlist from a string that contains comma separated values, if so, you can check here: http://sandeep-tada.blogspot.in/2012/09/populate-selected-items-of.html
asshole ..why no copy and past !! you suck
ReplyDelete