2012-04-03 86 views
9

我需要遍历一个选中的列表框,并且对于其中的每个项目,我需要检查它们(基本上就像是“全选”功能)。通过选中的列表框并检查所有项目C#

有没有一个基本的例子,你可以给我帮助我吗?

+0

可能你需要解释更多...这些复选框在哪里res ide ...在网格中?中继?清单?...请添加更多详细信息... – NiK 2012-04-03 19:25:02

+0

我同意NiK ..有很多方法取决于位置..如果它是在网页上.. jQuery是最简单的方法:D – Tony 2012-04-03 19:27:06

回答

22

使用SetSelected和interate通过所有的Items

// Loop through and set all to selected. 
for (int x = 0; x < listBox1.Items.Count; x++) 
{ 
    listBox1.SetSelected(x, true); 
} 

要检查的项目,使用SetItemChecked

// Loop through and set all to checked. 
for (int x = 0; x < listBox1.Items.Count; x++) 
{ 
    listBox1.SetItemChecked(x, true); 
} 
+0

非常感谢你,这但只有当该行更改为: listBox1.SetItemChecked(x,true); ,因为它是一个检查列表框 – user1290653 2012-04-03 19:31:55

+0

@ user1290653 - 你是对的,我会更新答案 – SwDevMan81 2012-04-03 19:34:56

5

您可以通过所有的项目看起来时listItems:

foreach (ListItem li in CheckBoxList1.Items) 
{ 
    li.Selected = true; 
} 
+4

匿名添加:(ListItem将需要对System.Web.UI.WebControls的引用) – mplungjan 2012-07-13 14:30:54

相关问题