【问题标题】:C# Selecting a CheckBoxList item(s) by matching against an Array of stringsC# 通过匹配字符串数组来选择 CheckBoxList 项
【发布时间】:2018-04-10 13:50:02
【问题描述】:

我正在努力寻找解决问题的方法,我花费了大量时间尝试替代解决方案,但无济于事。任何帮助或解释将不胜感激。

我有一个 SQL 数据库,它有一个名为“categories”的字符串字段,其中包含一个由“,”分隔的类别列表,例如转诊,门诊。

所以我希望将此列表与许多 CheckListBoxes 项目 (ID=CategoryCBL) 进行比较,只要类别与需要选择的 CheckListBox 项目匹配。

这是我的代码:

string categories = result.GetString(12).ToString();
string[] categorie = categories.Split(',');

 //loops through all seperated categories (cat) in categorie.
 foreach(string cat in categorie)
 {
     //loops through all list checkboxes 
     for(int index = 0; index <CategoryCBL.Items.Count; index++)
     {
         //gets the listcheck box string 
         string item = CategoryCBL.Items[index].ToString();
         //compare the list box string against the current Category looking for matches
         if (item == cat)
         {
             //if a match occures the list checkbox at that index is selected 
             CategoryCBL.SelectedIndex = index;

             TextBox1.Text += item + "-" + cat + "-" + index;
         }
     }
 }

这是我的检查列表框代码:

<asp:CheckBoxList ID="CategoryCBL" class="listItem" RepeatLayout="Table" RepeatColumns="2" RepeatDirection="Vertical" runat="server" Width="100%">
    <asp:ListItem>Referrals</asp:ListItem>
    <asp:ListItem>Outpatients</asp:ListItem>
    <asp:ListItem>Admissions/Discharges</asp:ListItem>
    <asp:ListItem>A&E</asp:ListItem>
    <asp:ListItem>Medical Records</asp:ListItem>
    <asp:ListItem>Outcome Form</asp:ListItem>
    <asp:ListItem>Data Quality</asp:ListItem>
    <asp:ListItem>Executive Reporting</asp:ListItem>
    <asp:ListItem>Infection Control</asp:ListItem>
    <asp:ListItem>Planning and Performance</asp:ListItem>
    <asp:ListItem>QlikView</asp:ListItem>
    <asp:ListItem>Theatres</asp:ListItem>
    <asp:ListItem>Waiting Times</asp:ListItem>
</asp:CheckBoxList>

所以我将我的类别设为 result.getString(12).ToString(); 在此示例中它等于 Infection Control,QlikView,Theatres

您还可以看到我已将结果打印到 TextBox1。

这是上面代码的结果

https://imgur.com/a/IwUqt

如您所见,仅选择了剧院。

https://imgur.com/y9sYNfW

TextBox1 中的输出显示在 X 索引处发生了 3 个匹配项,这些索引与我的各个检查列表框的索引对齐。

我真的很想知道为什么只选择了最后一个匹配复选框而不是前两个匹配项。

有什么想法吗?

谢谢。

【问题讨论】:

  • 您需要在问题本身中分享您的代码。图像不可读。您如何使用所有类别填充 CategoriesCBL?
  • “这是我的代码:i63.tinypic.com/2iactj7.png 不,stackoverflow 上的代码是文本而不是图像
  • 因此,如果这与其他控件类似,则您只有一个选定的索引。列表中的复选框是否具有可以设置为 true 的 IsChecked 属性?

标签: c# asp.net string-matching selectedindex checklistbox


【解决方案1】:

为您需要检查的每个项目设置 Selected 属性为 true。

foreach (ListItem item in CheckBoxList.Items)
{
    item.Selected = true;
}

在您的代码中:

//loops through all list checkboxes 
 for(int index = 0; index <CategoryCBL.Items.Count; index++)
 {
     //gets the listcheck box string 
     string item = CategoryCBL.Items[index].ToString();
     //compare the list box string against the current Category looking for matches
     if (item == cat)
     {
         //if a match occures the list checkbox at that index is selected 
         CategoryCBL.Items[index].Selected= true;

         TextBox1.Text += item + "-" + cat + "-" + index;
     }
 }

我认为这个问题很相似,可能有其他更好的答案Check multiple items in ASP.NET CheckboxList

【讨论】:

  • 感谢您的回答!有效。我以为我很接近了!
  • @HarryNicholls 是的,离得不远。我想它会是这样的,但我没有经常使用 asp.net,所以我花了一点时间才找到它。
【解决方案2】:

我认为你需要这样的东西。您将字符串拆分为 List,然后使用 Linq 检查项目是否在 CategoryCBL 中退出,然后选中复选框。

string categories = "Outcome Form, Executive Reporting, QlikView, Waiting Times";

List<string> categorie = categories.Split(',').ToList();

CategoryCBL.Items.Cast<ListItem>().ToList().ForEach(x => x.Selected = categorie.Any(y => y.Trim() == x.Value));

如果您需要在 TextBox 中选中的项目,您可以这样做

TextBox1.Text = String.Join(", ", CategoryCBL.Items.Cast<ListItem>().Where(x => x.Selected).Select(y => y.Value).ToList());

【讨论】:

    【解决方案3】:
    <asp:CheckBoxList ID="tolgraph" runat="server" RepeatLayout="Table" CssClass="cb" RepeatDirection="Horizontal">
        <asp:ListItem Text="Column" Value="column"></asp:ListItem>
        <asp:ListItem Text="Line" Value="line"></asp:ListItem>
        <asp:ListItem Text="Bar" Value="bar"></asp:ListItem>
        <asp:ListItem Text="Pie" Value="pie"></asp:ListItem>
        <asp:ListItem Text="Radar" Value="Radar"></asp:ListItem>
        <asp:ListItem Text="Pareto" Value="Pareto"></asp:ListItem>
    </asp:CheckBoxList>
    
    tolgraph.Items.FindByText("Column").Selected = true;
    

    【讨论】:

      猜你喜欢
      • 2015-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-11
      • 2022-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多