如何将checkboxlist中的选定项添加到字符串数组?

时间:2014-04-18 09:13:41

标签: c# asp.net arrays

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;
}

在这段代码中我想将checkboxlist选中的项目添加到字符串数组“selecteditems [i]”这里使用“selecteditems [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'");
                }
            } 

2 个答案:

答案 0 :(得分:0)

试试这个。

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'");
        }
    }
}

答案 1 :(得分:0)

你试试这样:

   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;
    }