2

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

我有一个 SQL 数据库,其中有一个名为“类别”的字符串字段,其中包含一个由“”分隔的类别列表,例如“推荐人”、“门诊患者”。

因此,我希望将此列表与许多 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 个匹配项,这些索引与我的各个检查列表框的索引对齐。

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

有任何想法吗?

谢谢你。

4

3 回答 3

3

对于需要检查的每个项目,将 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;
     }
 }

我认为这个问题很相似,可能有其他更好的答案检查 ASP.NET CheckboxList 中的多个项目

于 2018-04-10T14:29:37.990 回答
2

我想你需要这样的东西。您将字符串拆分为 a 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());
于 2018-04-10T14:28:29.663 回答
0
<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;
于 2019-07-03T06:14:28.813 回答