如何在for循环中获取动态创建的checkboxlist的Selected值

时间:2011-04-14 22:01:32

标签: asp.net dynamic c#-4.0 selecteditem checkboxlist

我有一个复选框列表,它是动态创建的,现在我想在for循环中获取checkboxlist的选定值,我的代码在下面

protected void OnbtnNext1_Click(object sender,EventArgs e)         {

        for (int i = 0; i < this.cbCountry.Items.Count; i++)
        {
            if (cbCountry.Items[i].Selected)
            {
                itemsCountry.Add(cbCountry.Items[i].Value);
                countCountry++;
            }
        }

        PopulateWaveCheckBoxes(itemsCountry);
        this.pnlWave.Visible = true;
    }


    private void PopulateWaveCheckBoxes(ArrayList items)
    {
        Label[] lbls = new Label[items.Count];
        CheckBoxList cblWave = new CheckBoxList[items.Count];

        for (int i = 0; i < items.Count; i++)
        {
            lbls[i] = new Label();
            lbls[i].Text = items[i].ToString();
            cblWave[i] = new CheckBoxList();
            cblWave[i].ID = "Checkbox" + i.ToString();
            cblWave[i].Items.Add(new ListItem("Wave 1"));
            cblWave[i].Items.Add(new ListItem("Wave 2"));
            cblWave[i].Items.Add(new ListItem("Wave 3"));
            cblWave[i].Items.Add(new ListItem("Wave 4"));
            this.pnlWave.Controls.Add(lbls[i]);
            this.pnlWave.Controls.Add(cblWave[i]);
            this.pnlWave.Controls.Add(new LiteralControl("<br>"));
        }
    }

    protected void OnbtnNext2_Click(object sender, EventArgs e)
    {

        itemsWave = new ArrayList[countCountry];
        for (int j = 0; j < countCountry; j++)
        {
            itemsWave[j] = new ArrayList();
            for (int i = 0; i < 4; i++)
            {
                if (cblWave[j].Items[i].Selected) // Here i want to get the values
                {
                    itemsWave[j].Add(cblWave[j].Items[i].Value);
                }
            }
            PopulateColorCheckBoxes(itemsWave[j], j);
        }
    }

1 个答案:

答案 0 :(得分:0)

您应该将CheckBoxList cblWave声明为类变量:

CheckBoxList cblWave;

然后在你的PopulateWaveCheckBoxes中你应该实例化并填写它:

private void PopulateWaveCheckBoxes(ArrayList items){
Label[] lbls = new Label[items.Count];
cblWave = new CheckBoxList[items.Count];