C# 选择 CheckBoxList 中的所有 CheckBox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9577350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select all CheckBoxes in CheckBoxList
提问by Cipher
I have a CheckBoxand a CheckBoxlist on my web page.
If the CheckBoxis selected, all the CheckBoxesin the CheckBoxListshould get selected, and if the CheckBoxis unchecked, similarly all the CheckBoxesin the CheckBoxshould get deselected (unchecked).
我有一个CheckBox和CheckBox我的网页列表。
如果CheckBox被选中,所有CheckBoxes的CheckBoxList应该得到选择,如果CheckBox未被选中,同样所有CheckBoxes的CheckBox应该得到取消选中状态(未选中)。
.aspx code
.aspx 代码
<asp:CheckBoxList ID="CheckBoxList1" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Flow">
<asp:ListItem>Item A</asp:ListItem>
<asp:ListItem>Item B</asp:ListItem>
<asp:ListItem>Item C</asp:ListItem>
<asp:ListItem Selected="True">Item D</asp:ListItem>
<asp:ListItem>Item E</asp:ListItem>
<asp:ListItem>Item F</asp:ListItem>
<asp:ListItem>Item G</asp:ListItem>
</asp:CheckBoxList>
<asp:CheckBox ID="allChkBox" Text="Select all" runat="server"
oncheckedchanged="allChkBox_CheckedChanged" />
I tried by doing somehting like this, but it didb't work:
我尝试做这样的事情,但没有奏效:
bool prevSelection = false;
protected void allChkBox_CheckedChanged(object sender, EventArgs e)
{
if (!prevSelection)
{
foreach (ListItem chkitem in CheckBoxList1.Items)
{
chkitem.Selected = true;
}
}
else
{
foreach (ListItem chkitem in CheckBoxList1.Items)
{
chkitem.Selected = false;
}
}
prevSelection = !prevSelection;
}
采纳答案by jaySF
I prefer to use client script for something like this so your page doesnt have to do a postback
我更喜欢使用客户端脚本来做这样的事情,所以你的页面不必做回发
If that is a possibility try firing a javascript function on click to do the looping and selecting ... something like
如果有这种可能性,请尝试在单击时触发 javascript 函数以执行循环和选择……类似的操作
<script type="text/javascript">
checked=false;
function checkedAll (frm1) {
var aa= document.getElementById('frm1');
if (checked == false)
{
checked = true
}
else
{
checked = false
}
for (var i =0; i < aa.elements.length; i++)
{
if(aa.elements[i].type == 'checkbox') {
aa.elements[i].checked = checked;
}
}
}
</script>
回答by BACON
It's been a while since I've dabbled in ASP.NET, but your prevSelectionfield will be initialized to falseon each and every request. That value will not be persisted between requests. So, you either need to store it in View State or the cache and load it from there in your event handler, or, even better, change your method to something like this:
自从我涉足 ASP.NET 已经有一段时间了,但是您的prevSelection字段将false在每个请求上初始化。该值不会在请求之间保持不变。因此,您要么需要将它存储在视图状态或缓存中并从那里加载到您的事件处理程序中,或者更好的是,将您的方法更改为如下所示:
protected void allChkBox_CheckedChanged(object sender, EventArgs e)
{
foreach (ListItem chkitem in CheckBoxList1.Items)
{
chkitem.Selected = allChkBox.Selected;
}
}
回答by Chris Young
Instead of using a variable outside the function, how about using the checkbox itself:
与其在函数外使用变量,不如使用复选框本身:
protected void allChkBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkbox = sender;
foreach (ListItem chkitem in CheckBoxList1.Items)
{
chkitem.Selected = chkbox.Selected;
}
}
回答by COLD TOLD
you can do it with linq like this
你可以像这样用 linq 做到这一点
var allChecked = (from ListItem item in CheckBoxList1.Items
where item.Selected
select int.Parse(item.Value)).ToList();
var all = (from ListItem item in CheckBoxList1.Items
select int.Parse(item.Value)).ToList();
回答by Rocky
function CheckUnCheckAll()
{
var list = document.getElementById("<%=DataList1.ClientID%>") ;
var chklist = list.getElementsByTagName("input");
for (var i=0;i<chklist.length;i++)
{
if (chklist[i].type=="checkbox" )
{
chklist[i].checked = checkoruncheck;
}
}
}
回答by Subrata Sarkar
How about this Iif I have understood the requirement right!)? This will make all items selectedin a CheckBoxList control by default when it renders:
如果我已经理解了要求,这个怎么样!)?这将使selectedCheckBoxList 控件中的所有项目在呈现时默认为:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack) return;
LoadCountryList();
}
private void LoadCountryList()
{
_ctx = new PayLinxDataContext();
chkCountries.DataSource = _ctx.Countries.OrderBy(c => c.Name);
chkCountries.DataBind();
foreach (ListItem item in chkCountries.Items)
{
item.Selected = true;
}
}

