2012-01-17 87 views
0

我有一个Repeater控件创建动态数量CheckBoxList控件,每个列表都有一组不同的ListItems引用中继器中的复选框

我已经完成了这部分,但我遇到的问题是如何保存这些动态创建的框的选中状态。我找不到如何获取这些CheckBoxList控件的列表。

这里是我想要做一些伪代码:

foreach (Item i in MyRepeater) 
{ 
    if (i.ItemType is CheckBoxList) 
    { 
     foreach (ListItem x in i) 
     { 
      update table set tiChecked = x.Checked 
      where table.id = i.id and table.typeid = x.id 
     } 
    } 
} 

我有CheckBoxList和对应于数据库的ID的ListItem的ID。

编辑:

当然我问之后,我发现它。这似乎得到我我想要的

foreach (RepeaterItem tmp in rptReportList.Items) 
{ 
    if (tmp.ItemType == ListItemType.Item) 
    { 
     foreach (Control c in tmp.Controls) 
     { 
      if (c is CheckBoxList) 
      { 
       DisplayMessage(this, c.ID.ToString()); 
      } 
     } 
    } 
} 
+0

什么,这部分是不工作? foreach(我在ListItem x中)? – TheGeekYouNeed 2012-01-17 16:07:40

+0

这只是我想要做的伪代码。 MyRepeater返回的项目只是RepeaterItems。这些项目似乎没有任何子项目,所以我无法弄清楚如何枚举我的CheckBoxLists。 – Bengie 2012-01-17 16:11:47

+0

我想说,我想如果你采用你的伪代码的方法,你将不得不把我作为CheckBoxList ...所以foreach(ListItem x in(我作为CheckBoxList).Items) – TheGeekYouNeed 2012-01-17 16:39:38

回答

3

你需要比RepeaterItem再深入:

// repeater item 
foreach (Control cr in MyRepeater.Controls) 
{ 
    // controls within repeater item 
    foreach (Control c in cr.Controls) 
    { 
     CheckBoxList chklst = c as CheckBoxList; 
     if (chklst != null) 
     { 
      foreach (ListItem i in chklst.Items) 
      { 
       string valueToUpdate = i.Value; 
       string textToUpdate = i.Text; 
       bool checkedToUpdate = i.Selected; 

       // Do update 
      } 
     } 
    } 
}