从分隔数据库字符串字段中选择多选列表框中的项目

发布于 2024-07-13 14:35:41 字数 1127 浏览 5 评论 0原文

我正在尝试为数据库记录构建一个“编辑”页面,可以编辑该页面并将其保存回数据库。 其中一个字段是一个多选列表框,加载时需要突出显示硬编码列表中的相应列表项。

使用 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>

感谢大家。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

做个少女永远怀春 2024-07-20 14:35:41

我会提出一些类似的建议。 它似乎比嵌套循环更具可读性。

    List<string> categories = new List<string>(reader["CATEGORY"].ToString().Split(','));
    foreach (ListItem item in CATEGORYListBox.Items)
    {
        if (categories.Contains(item.Value))
            item.Selected = true;
    }

I would suggest something along these lines. It seems more readable than doing nested loops.

    List<string> categories = new List<string>(reader["CATEGORY"].ToString().Split(','));
    foreach (ListItem item in CATEGORYListBox.Items)
    {
        if (categories.Contains(item.Value))
            item.Selected = true;
    }
鹤仙姿 2024-07-20 14:35:41

这是蛮力且丑陋的,但它应该有效。 看起来您上面的代码是 VB 和 C# 之间的某种混合体。 下面的代码仅是 C#。 另外,请考虑不要在代码隐藏中执行 ADO.Net。

for (int i = 0; i < CATEGORYListBox.Items.Count; i++)
{
    foreach (string category in reader["CATEGORY"].ToString().Split(','))
    {
        if (category != CATEGORYListBox.Items[i].Value) continue;
        CATEGORYListBox.Items[i].Selected = true;
        break;
    }
}

This is brute force and ugly, but it should work. It looks like your code above is some sort of hybrid between VB and C#. The code below is C# only. Also, consider not doing your ADO.Net in your codebehind.

for (int i = 0; i < CATEGORYListBox.Items.Count; i++)
{
    foreach (string category in reader["CATEGORY"].ToString().Split(','))
    {
        if (category != CATEGORYListBox.Items[i].Value) continue;
        CATEGORYListBox.Items[i].Selected = true;
        break;
    }
}
爱格式化 2024-07-20 14:35:41

最简单的实现?

string sequenceFromDBorPostBack= "1,3,4,6,48";

foreach (ListItem item in lstLocations.Items)
{
     item.Selected = sequenceFromDBorPostBack.Split(',').Contains(item.Value);
}

我不确定这是否有效。

The easiest implementation?

string sequenceFromDBorPostBack= "1,3,4,6,48";

foreach (ListItem item in lstLocations.Items)
{
     item.Selected = sequenceFromDBorPostBack.Split(',').Contains(item.Value);
}

I am not sure if this is performance effective..

少女情怀诗 2024-07-20 14:35:41

该问题的另一个解决方案是:

    string[] arrSplitItems;
    arrSplitItems = TestsOrdrd.Split(',');
    if (arrSplitItems.Length > 0)
    {
        for (int iCount = 0; iCount < arrSplitItems.Length; iCount++)
        {
            lstTestcode.Items.FindByValue(arrSplitItems[iCount].ToString()).Selected = true;
        }
    }

TestsOrdrd 包含列表框的选定值。

谢谢,
拉提卡·克里希纳维卢

Another Solution for the problem is:

    string[] arrSplitItems;
    arrSplitItems = TestsOrdrd.Split(',');
    if (arrSplitItems.Length > 0)
    {
        for (int iCount = 0; iCount < arrSplitItems.Length; iCount++)
        {
            lstTestcode.Items.FindByValue(arrSplitItems[iCount].ToString()).Selected = true;
        }
    }

TestsOrdrd contains the Selected values of Listbox.

Thanks,
Rathika Krishnavelu

飘然心甜 2024-07-20 14:35:41
ListBox1.SelectionMode = ListSelectionMode.Multiple;
string[] pageRoles = myReader["UserProfile"].ToString().Split(',').ToArray();
pageRoles.ToList().ForEach(r => ListBox1.Items.FindByValue(r).Selected = true);
ListBox1.SelectionMode = ListSelectionMode.Multiple;
string[] pageRoles = myReader["UserProfile"].ToString().Split(',').ToArray();
pageRoles.ToList().ForEach(r => ListBox1.Items.FindByValue(r).Selected = true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文