当我取消选中Child CheckBox时,HeaderCheckBox未取消选中?是什么原因? [英] When i Unchecked Child CheckBox then HeaderCheckBox is not Unchecked? What is the reason?
本文介绍了当我取消选中Child CheckBox时,HeaderCheckBox未取消选中?是什么原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是C#代码..
受保护 void ChkAll_CheckedChanged( object sender,EventArgs e)
{
CheckBox ChkAll =(CheckBox)GridView1.HeaderRow.FindControl( ChkAll);
if (ChkAll.Checked == true )
{
for ( int i = 0 ; i < GridView1.Rows.Count; i ++)
{
CheckBox chk =(CheckBox)GridView1.Rows [i]。 FindControl( ChkOne);
chk.Checked = true ;
}
}
else
{
for ( int i = 0 ; i < GridView1.Rows.Count; i ++)
{
CheckBox chk =(CheckBox)GridView1.Rows [i] .FindControl( ChkOne);
chk.Checked = false ;
ChkAll.Checked = false ;
}
}
}
}
这是我的ASPX代码
< asp:GridView ID = GridView1 runat = server AutoGenerateColumns = False
CssClass < span class =code-keyword> = data 宽度 = 50% AllowSorting = True DataSourceID = < span class =code-keyword> dsDetails
< span class =code-attribute> AllowPaging = True PageSize = 5 >
< 列 >
< asp:TemplateField >
< HeaderTemplate >
< asp:CheckBox ID = ChkAll runat = server AutoPostBack = true oncheckedchanged = ChkAll_CheckedChanged / >
< / HeaderTemplate >
< ItemTemplate >
< asp:CheckBox ID = ChkOne runat = server AutoPostBack = true / >
< / ItemTemplate >
< span class =code-keyword>< / asp:TemplateField >
解决方案
在.cs页面上创建此活动
------------ -----------------
protected void ChkOne_CheckedChanged( object sender,EventArgs e)
{
bool flag = true ;
for ( int i = 0 ; i < GridView1.Rows.Count; i ++)
{
CheckBox chk =(CheckBox)GridView1.Rows [i]。 FindControl( ChkOne);
if (!chk.Checked)
{
flag = false ;
break ;
}
}
((CheckBox)GridView1.HeaderRow.FindControl( ChkAll ))。Checked = flag;
}
替换.aspx页面上的项目模板代码
----------------------------------------
< asp:GridView ID = GridView1 runat = server AutoGenerateColumns = False
CssClass = data 宽度 = < span class =code-keyword> 50% AllowSorting = True DataSourceID = dsDetails
AllowPaging = True PageSize = 5 >
< asp:TemplateField >
< HeaderTemplate >
< asp:CheckBox ID = ChkAll runat = server AutoPostBack = true oncheckedchanged < span class =code-keyword> = ChkAll_CheckedChanged / >
< / HeaderTemplate >
< ItemTemplate < span class =code-keyword>>
< asp:CheckBox ID = ChkOne runat = server AutoPostBack = true oncheckedchanged = ChkOne_CheckedChanged / >
< / ItemTemplate >
< / asp :TemplateField >
< / asp:GridView > 跨度>
Here Is C# Code..
protected void ChkAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox ChkAll = (CheckBox)GridView1.HeaderRow.FindControl("ChkAll");
if (ChkAll.Checked == true)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("ChkOne");
chk.Checked = true;
}
}
else
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("ChkOne");
chk.Checked = false;
ChkAll.Checked = false;
}
}
}
}
THis is my ASPX Code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CssClass="data" Width="50%" AllowSorting="True" DataSourceID="dsDetails"
AllowPaging="True" PageSize="5">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="ChkAll" runat="server" AutoPostBack="true" oncheckedchanged="ChkAll_CheckedChanged"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="ChkOne" runat="server" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
解决方案
create this event on .cs page
-----------------------------
protected void ChkOne_CheckedChanged(object sender, EventArgs e) { bool flag = true; for (int i = 0; i < GridView1.Rows.Count; i++) { CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("ChkOne"); if(!chk.Checked) { flag = false; break; } } ((CheckBox)GridView1.HeaderRow.FindControl("ChkAll")).Checked = flag; }
replace item template code on .aspx page
----------------------------------------
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="data" Width="50%" AllowSorting="True" DataSourceID="dsDetails" AllowPaging="True" PageSize="5"> <asp:TemplateField> <HeaderTemplate> <asp:CheckBox ID="ChkAll" runat="server" AutoPostBack="true" oncheckedchanged="ChkAll_CheckedChanged"/> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="ChkOne" runat="server" AutoPostBack="true" oncheckedchanged="ChkOne_CheckedChanged"/> </ItemTemplate> </asp:TemplateField> </asp:GridView>
这篇关于当我取消选中Child CheckBox时,HeaderCheckBox未取消选中?是什么原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文