<October 2005>
SuMoTuWeThFrSa
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

Post Categories

Navigation

Subscriptions

[asp.net] Simple utility function to return all selected values from a CheckBoxList

The ASP.NET CheckBoxList.SelectedValue property only returns first item selected. The MSDN solution is ugly - iterate the CBL items checking each one to see if it's selected.

Five minutes of Google-Fu didn't turn up anything, so here's a simple utility function to get a string array of selected values.

public string[] CheckboxListSelections(System.Web.UI.WebControls.CheckBoxList list)
{
 ArrayList values = 
new ArrayList();
 
for(int counter = 0; counter < list.Items.Count; counter++)
 {
  
if(list.Items[counter].Selected)
  {
   values.Add(list.Items[counter].Value);
  }    
 }
 
return (String[]) values.ToArray( typeofstring ) );
}

Posted so:

  1. I can find it later
  2. I can maybe save the next guy some time
  3. The piranha haters community can point out how this could be done better.

posted on Sunday, October 02, 2005 2:24 AM by admin

Powered by Community Server, by Telligent Systems