我正在尝试为可以编辑并保存回数据库的数据库记录构建一个“编辑”页面。其中一个字段是多选列表框,加载时需要在硬编码列表中突出显示适当的列表项。
使用 C#,我如何根据数据库字段中的逗号分隔字符串填充多选列表框 - 并选择适当的项目?我研究了一些涉及循环的解决方案,但我无法让它们与我有限的 C# 技能组一起工作。
这就是我现在所拥有的,在我陷入困境之前。您会看到它没有考虑字符串中的多个值。是否有像“包含”这样的功能,我可以检查值是否匹配?我在这里仍然缺少一些(可能是基本的)C# 逻辑和编码。
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>
谢谢大家。