如何选择gridview中的所有复选框? [英] How to select all checkbox in gridview?
本文介绍了如何选择gridview中的所有复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的代码不适用于全选复选框。点击选择全部复选框后,所有复选框都保持未选中状态。
我尝试了什么:
Below code doesn't work for select all checkbox. after clicking on select all checkbox,all checkboxes remain unchecked.
What I have tried:
<asp:GridView ID="grdMember" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" HorizontalAlign="Center">
<columns>
<asp:TemplateField HeaderText="Select All">
<headertemplate>
<asp:CheckBox ID="checkbox2" OnCheckedChanged="CheckAll" runat="server" />
<itemtemplate>
<asp:CheckBox ID="IDCheckbox" runat ="server" />
protected void CheckAll(object sender, EventArgs e)
{
CheckBox chckheader = (CheckBox)grdMember.HeaderRow.FindControl("checkbox2");
foreach (GridViewRow row in grdMember.Rows)
{
CheckBox chckrw = (CheckBox)row.FindControl("IDCheckbox");
if (chckheader.Checked == true)
{
chckrw.Checked = true;
}
else
{
chckrw.Checked = false;
}
}
}
推荐答案
Quote:
< asp:CheckBox ID =checkbox2OnCheckedChanged =CheckAllrunat =server/>
<asp:CheckBox ID="checkbox2" OnCheckedChanged="CheckAll" runat="server" />
你的 CheckAll
方法永远不会被解雇,因为你没有设置 AutoPostback
到 True
为您的 CheckBox
。将AutoPostback设置为true,这样就会触发 OnCheckedChanged
事件。
Your CheckAll
method will never get fired because you haven't set AutoPostback
to True
for your CheckBox
. Set AutoPostback to true so the OnCheckedChanged
event will fire.
<asp:CheckBox ID="checkbox2" OnCheckedChanged="CheckAll" runat="server" AutoPostBack="True" />
这篇关于如何选择gridview中的所有复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文