繁体   English   中英

C#通过与字符串数组匹配来选择一个CheckBoxList项目

[英]C# Selecting a CheckBoxList item(s) by matching against an Array of strings

我正在努力寻找解决方案,我花了很多时间尝试其他解决方案,但无济于事。 任何帮助或解释将不胜感激。

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

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

这是我的代码:

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(); 在此示例中,它等于感染控制,QlikView,剧院

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

这是上面代码的结果

https://imgur.com/a/IwUqt

如您所见,只有剧院被选中。

https://imgur.com/y9sYNfW

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

我真的很想知道为什么仅选择了“最后一场比赛”复选框而不选择previos 2比赛。

有任何想法吗?

谢谢。

将需要检查的每个项目的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中的多个项目

我认为您需要这样的东西。 您将字符串拆分为一个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());
<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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM