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

时间:2018-04-10 13:50:03

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

我很难找到问题的解决方案,我花了相当多的时间尝试其他解决方案无济于事。任何帮助或解释将不胜感激。

我有一个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(); 在这个例子中它等于感染控制,QlikView,剧院

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

以上是上述代码的结果

https://imgur.com/a/IwUqt

正如您所看到的那样,只选择了剧院。

https://imgur.com/y9sYNfW

TextBox1中的输出显示在X索引处有3个匹配项,这些索引与我的各个核对表框的索引对齐。

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

任何想法?

谢谢。

3 个答案:

答案 0 :(得分: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;
     }
 }

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

答案 1 :(得分: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());

答案 2 :(得分:0)

    WebSettings webSettings = mywebview.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setAppCacheEnabled(true);
    webSettings.setSupportZoom(true);
    mywebview.loadUrl("https://code.org");
    mywebview.setWebViewClient(new WebViewClient());
    mywebview.setLoadWithOverviewMode(true);
相关问题