I would like to concatenate the checked values of a CheckBoxList
. I am using the following code which works fine in the form's code behind:
string sFooType = "";
for (int i = 0; i < chkFooTypes.Items.Count; i++)
{
if (chkFooTypes.Items[i].Selected)
{
if (sFooType == "")
sFooType = chkFooTypes.Items[i].Text;
else
sFooType += "," + chkFooTypes.Items[i].Text;
}
}
However, I would like to put this code in its own class to be called when required. The same CheckBoxList
appears on two different forms - I'm trying not to repeat code. I know I'm being a bit pedantic, but its the only way to learn!
Could I populate a public list and then concatenate the list? Where I'm stumped is how the class will know which control/form to work with.
I did try to adapt this solution, but I couldn't get my head around it. I could see how it worked for textbox, but not how it would work for a CheckBoxList
.