微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在for循环中获取动态创建的复选框列表的选定值

如何解决如何在for循环中获取动态创建的复选框列表的选定值

|| 我有动态创建的checkBoxlist数组,现在我想在for循环中获取checkBoxlist的选定值,我的代码如下 受保护的void OnbtnNext1_Click(对象发送者,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声明为类变量:
CheckBoxList cblWave;
然后在
PopulateWaveCheckBoxes
中实例化并填充它:
private void PopulateWaveCheckBoxes(ArrayList items){
Label[] lbls = new Label[items.Count];
cblWave = new CheckBoxList[items.Count];
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。