SoFunction
Updated on 2025-03-06

C# traversal to get the checkboxlist selected value and set the selected item

Get the value of the selected item and splice it into a string to return

public string GetChecked(CheckBoxList checkList, string separator)
{
string str = "";
for (int i = 0; i < ; i++)
{
if ([i].Selected)
{
str += [i].Value + separator;
}
}
return str;
}

There is a selected string. After dividing, traversal and select the corresponding value value option.

public void SetChecked(CheckBoxList checkList, string selval, string separator)
{
selval = separator + selval + separator; //For example: "0,1,1,2,1"->",0,1,1,2,1,"for (int i = 0; i &lt; ; i++)
{
[i].Selected = false;
string val = separator + [i].Value + separator;
if ((val) != -1)
{
[i].Selected = true;
selval = (val, separator); //Then delete the selected one from the original value stringif (selval == separator) // If the last item of selval is also selected, only one separator will be left after replacement.{
selval += separator; //Add a separator}
}
}
}

The above is the code for obtaining checkboxlist selected values ​​and setting selected items introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!