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

发布于 2024-11-01 19:08:03 字数 1886 浏览 4 评论 0原文

我有动态创建的复选框列表数组,现在我想在 for 循环中获取复选框列表的选定值,我的代码位于

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

I have Array of checkboxlist, which is dynamically created, now i want to get selected values of checkboxlist in for loop , my code is below

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画▽骨i 2024-11-08 19:08:03

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

CheckBoxList cblWave;

然后在您的 PopulateWaveCheckBoxes 中您应该实例化并填充它:

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

You should declare your CheckBoxList cblWave as a class variable:

CheckBoxList cblWave;

And then in your PopulateWaveCheckBoxes you should instantiate and fill it:

private void PopulateWaveCheckBoxes(ArrayList items){
Label[] lbls = new Label[items.Count];
cblWave = new CheckBoxList[items.Count];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文