【发布时间】: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。
这是上面代码的结果
如您所见,仅选择了剧院。
TextBox1 中的输出显示在 X 索引处发生了 3 个匹配项,这些索引与我的各个检查列表框的索引对齐。
我真的很想知道为什么只选择了最后一个匹配复选框而不是前两个匹配项。
有什么想法吗?
谢谢。
【问题讨论】:
-
您需要在问题本身中分享您的代码。图像不可读。您如何使用所有类别填充 CategoriesCBL?
-
“这是我的代码:i63.tinypic.com/2iactj7.png” 不,stackoverflow 上的代码是文本而不是图像
-
因此,如果这与其他控件类似,则您只有一个选定的索引。列表中的复选框是否具有可以设置为 true 的 IsChecked 属性?
标签: c# asp.net string-matching selectedindex checklistbox