From your description and code it looks like you have a space in one value you're comparing but not the other - this whitespace may not be obvious when viewing it in the debugger which is why you think it's not working.
You show an example of using "1, 2, 4" and you are splitting your command argument string by a comma deliminator. This will lead to values "1", " 2" and " 4" in ca (and therefore FormDetails_ID). The comparison against "1" will work for the first form but comparing " 2" against "2" will not work as intended.
If this is the case then a potential solution is quite simple - just trim the string when you assign it to FormDetails_ID:
FormDetails_ID = ca.Trim();
Note - I would also suggest you may want to check the logical comparison you're making ca. i.e. "if(ca != null
|| ca != string.Empty
|| ca != "")" will allow an empty string to be checked since it won't be null. A simple change would be to use logical AND rather than logical OR:
if(ca != null & ca != string.Empty & ca != "")
{
FormDetails_ID = ca.Trim();
if (item.Value == FormDetails_ID)
{
item.Selected = true;
}
}
You could also use the in-built static string check function IsNullOrEmpty for this instead:
foreach (var ca in commandArgs)
{
if(false == string.IsNullOrEmpty(ca))
{
FormDetails_ID = ca.Trim();
if (item.Value == FormDetails_ID)
{
item.Selected = true;
}
}
}
If you want to go the whole way then you could simplify it further and not even have empty elements returned by the split function and ensure each item is selected or un-selected explicitly by an assignment:
chkFormNames.Items.Add(item);
string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var ca in commandArgs)
{
item.Selected = (item.Value == ca.Trim());
}