日期:2014-05-18  浏览次数:20472 次

gridview全选按钮的问题
[code=C#protected void allchck_CheckedChanged(object sender, EventArgs e)
  {
  foreach (GridViewRow gr in GV.Rows)
  {
  CheckBox ck=(CheckBox)gr.Cells[6].FindControl("itemchck");

   
  if (!ck.Checked)
  {
  ck.Checked = true;

  }
  else
  {
  ck.Checked = false;
  }
   

  }

  }][/code]

这个我判断了,gridview里的全选按钮,可是,我单个选择一个时,我在点击全选就会取反。而不是全部选中。怎么决绝呢


------解决方案--------------------
**.aspx页面: 
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
Width="373px" DataKeyNames="PackageID" Font-Names="宋体" Font-Size="9pt">
<Columns>
<asp:BoundField DataField="CompanyID" HeaderText="公司ID" />
<asp:BoundField DataField="ProductID" HeaderText="产品ID" />
<asp:BoundField DataField="PackageID" HeaderText="产品包ID" />
<asp:BoundField DataField="PackageName" HeaderText="产品名称" />
<asp:BoundField DataField="PackagePrice" HeaderText="价格" />
<asp:HyperLinkField DataNavigateUrlFields="ProductID,PackageID" 
DataNavigateUrlFormatString="Products_MenuList.aspx?ProductID={0}&amp;&amp;PackageID={1}" 
HeaderText="详细目录" Text="详细目录" Target="_blank" />
<asp:TemplateField FooterText="全选" HeaderText="选择">
<HeaderTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" Text="全选" OnCheckedChanged="CheckAll" />   
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" Font-Names="宋体" Font-Size="9pt" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

**.aspx.cs页面:
#region GridView全选/取消
protected void CheckAll(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;

if (cb.Text == "全选")
{
foreach (GridViewRow gv in this.GridView1.Rows)
{
CheckBox cd = (CheckBox)gv.FindControl("CheckBox1");
cd.Checked = cb.Checked;
cb.Text = "取消";
}
}
else
{
cb.Text = "取消";
foreach (GridViewRow gv in this.GridView1.Rows)
{
CheckBox cd = (CheckBox)gv.FindControl("CheckBox1");
cd.Checked = cb.Checked;
cb.Text = "全选";
}