如何将选中的项目从复选框列表添加到字符串数组

本文关键字:列表 复选框 添加 数组 字符串 项目 | 更新日期: 2023-09-27 17:58:56

public string[] selected()
{
    string[] selecteditems = new string[0];
    for (int i = 0; i < chbindustry.Items.Count-1; i++)
    {
        if (chbindustry.Items[i].Selected)
        {
            selecteditems[i] = chbindustry.Items[i].Text.ToString();

            //string Va = string.Empty;
            //Va = chbindustry.Items[i].Text.ToString();
           // selecteditems[i] = Va;
        }
    }
    return selecteditems;
}

在这段代码中,我想使用"selecteditems[I]"将复选框列表中的选定项目添加到字符串数组"selectditems[I]"中。我需要将其绑定到下面的代码中,并仅显示选定项目

foreach (string s in subdirectoryEntries)
            {
                DirectoryInfo d = new DirectoryInfo(s);
                for (int i = 1; i <= d.GetFiles().Length / 3; i++)
                {
                    selected();
                    Page.ClientScript.RegisterArrayDeclaration("ImgPaths", "'" + "BusinessCards/" + s.Remove(0, s.LastIndexOf('''') + 1) + "/" + i + ".jpg'");
                    Page.ClientScript.RegisterArrayDeclaration("refs", "'" + "DesignBCs.aspx?img=BusinessCards/" + s.Remove(0, s.LastIndexOf('''') + 1) + "/" + i + "&Side=2'");
                }
            } 

如何将选中的项目从复选框列表添加到字符串数组

试试这个。

public string[] selected()
{
    string strTemp = "";
    for (int i = 0; i < chbindustry.Items.Count - 1; i++)
    {
        if (chbindustry.Items[i].Selected)
        {
            strTemp += chbindustry.Items[i].Text.ToString() + ",";
        }
    }
    string[] selecteditems = strTemp.Split(','); 
    return selecteditems;
}

。。。。。。。。。编辑1:

string[] selecteditems = selected();
foreach (string s in subdirectoryEntries)
{
    if (!string.IsNullOrEmpty(s) && selecteditems.Contains(s)) //Folder is selected in ListItem
    {
        DirectoryInfo d = new DirectoryInfo(s);
        for (int i = 1; i <= d.GetFiles().Length / 3; i++)
        {
            selected();
            Page.ClientScript.RegisterArrayDeclaration("ImgPaths", "'" + "BusinessCards/" + s.Remove(0, s.LastIndexOf('''') + 1) + "/" + i + ".jpg'");
            Page.ClientScript.RegisterArrayDeclaration("refs", "'" + "DesignBCs.aspx?img=BusinessCards/" + s.Remove(0, s.LastIndexOf('''') + 1) + "/" + i + "&Side=2'");
        }
    }
}