【问题标题】:Selecting items in multi-select listbox from delimited database string field从分隔的数据库字符串字段中选择多选列表框中的项目
【发布时间】:2009-02-10 13:17:22
【问题描述】:

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

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

谢谢大家。

【问题讨论】:

    标签: c# asp.net database listbox multi-select


    【解决方案1】:

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

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

    【讨论】:

    • Jesse - 我也试过你的,但我一直收到以下错误:“CS0246:找不到类型或命名空间名称“列表”(您是否缺少 using 指令或程序集引用? )" 因为我对 C3 有点太陌生了,所以我不知道从那里去哪里。不过还是谢谢!
    • 我相信您当然需要使用 System.collections.Specialized leewebdev,或者完全符合条件的 List
    • List 是 System.Collections.Generic 命名空间的一部分。为此添加一个 using 指令,你应该没问题。使用列表的好处是它使代码更具可读性,虽然开销很小,但我不认为这是一个问题,因为我们不应该预先优化。 :)
    • peacedog 和 Michael 是正确的,List 在 System.Collections.Generics 中。如果你不使用它们来解决这个问题,你仍然应该花一些时间来学习它们。它们是框架的一个非常重要的特性。
    【解决方案2】:

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

    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;
        }
    }
    

    【讨论】:

    • Michael - 这与两个小修复一起工作,1)我必须在第一行的 .Count 附加 -1 否则它会引发超出范围的错误,以及 2)我必须使用拆分命令中的单引号不是双引号。之后它就像一个魅力,谢谢!
    【解决方案3】:

    最简单的实现方式?

    string sequenceFromDBorPostBack= "1,3,4,6,48";
    
    foreach (ListItem item in lstLocations.Items)
    {
         item.Selected = sequenceFromDBorPostBack.Split(',').Contains(item.Value);
    }
    

    我不确定这是否有效..

    【讨论】:

    • 在循环中包含 Split() 可能不是最有效的。如果列表框已预先填充,您将必须循环遍历每个项目,如图所示。如果您正在动态填充列表列表,则选择检查可能在该循环中而不是单独的循环中。
    • 谢谢布拉德,这是有道理的。我将在我的代码中检查它。我想我的解决方案非常好,代码很干净,但仅在某些情况下有用。
    【解决方案4】:

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

        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 包含列表框的选定值。

    谢谢, Rathika Krishnavelu

    【讨论】:

      【解决方案5】:
      ListBox1.SelectionMode = ListSelectionMode.Multiple;
      string[] pageRoles = myReader["UserProfile"].ToString().Split(',').ToArray();
      pageRoles.ToList().ForEach(r => ListBox1.Items.FindByValue(r).Selected = true);
      

      【讨论】:

        猜你喜欢
        • 2018-05-06
        • 1970-01-01
        • 1970-01-01
        • 2016-10-25
        • 2023-01-03
        • 1970-01-01
        • 2016-11-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多