2011-04-14 21 views
0

我的CheckBoxList的阵列,可以动态创建的,现在我想在for循环的CheckBoxList所选择的值,我的代码如下如何获得选定的动态创建的CheckBoxList的值在循环

保护无效OnbtnNext1_Click (对象发件人,EventArgs的){

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

回答

0

你应该申报的CheckBoxList cblWave为类变量:

CheckBoxList cblWave; 

,然后在PopulateWaveCheckBoxes你应该实例并填写:

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

感谢Dampe,但我已宣布它作为一个类变量。我正在阅读一篇文章,以在运行时坚持checkboxlist的值,可以通过ViewState。但与ViewState的问题是,它不允许非序列化的对象。由于CheckBoxList是不可序列化的,我现在必须标记为可序列化,但为此,可能需要为复选框列表创建另一个可序列化的类。但我不确定如何制作,或者这是唯一的方法。 – Syed 2011-04-15 15:56:29

+0

使用DynamicPlaceHolder http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx – Syed 2011-09-07 16:49:18

相关问题