I'm trying to build an "edit" page for a database record that can be edited and saved back to the database. One of the fields will be a multi-select listbox that will need to highlight the appropriate list items in a hard-coded list when loaded.
Using C#, how do I populate a multi-select listbox -- with the appropriate items selected -- based on the comma-delimited string from a database field? I've researched a few solutions that involve loops, but I have been unable to get them to work with my limited C# skillset.
This is all I have now, before I got stuck. You'll see that it doesn't account for multiple values in the string. Is there a function like "contains" that I can check to see if the value matches? I'm still missing some (probably basic) C# logic and coding here.
int i;
for (i = 0; i <= CATEGORYListBox.Items.Count - 1; i++)
{
if (reader["CATEGORY"].ToString() == CATEGORYListBox.Items(i).Value)
{
CATEGORYListBox.Items(i).Selected = True;
}
}
...
<asp:ListBox ID="CATEGORYListBox" runat="server">
<asp:ListItem Value="Circulation">Circulation</asp:ListItem>
<asp:ListItem Value="Interactive Media">Interactive Media</asp:ListItem>
<asp:ListItem Value="Classified">Classified</asp:ListItem>
<asp:ListItem Value="Publishing">Publishing</asp:ListItem>
<asp:ListItem Value="Editorial">Editorial</asp:ListItem>
<asp:ListItem Value="Retail">Retail</asp:ListItem>
</asp:ListBox>
Thanks everyone.