ASPxGridView利用CheckBox实现全选

转自

http://kongjian.baidu.com/hjs%C2%FE%B2%BD%D4%C6%B6%CB/blog/item/273b34344a0aa348251f1448.html

 

cs

 

代码
using  System;
using  System.Collections;
using  System.Configuration;
using  System.Data;
using  System.Linq;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.HtmlControls;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Xml.Linq;

using  DevExpress.Web.ASPxGridView;


public   partial   class  结合checkbox_Default : System.Web.UI.Page
{
    
protected   void  Page_Load( object  sender, EventArgs e)
     {

     }
    
protected   void  selectAll_CheckedChanged( object  sender, EventArgs e)
     {
        
int  startid  =  ASPxGridView1.PageIndex  *  ASPxGridView1.SettingsPager.PageSize;
        
int  endid;

        
if  (ASPxGridView1.PageIndex  ==  ASPxGridView1.PageCount  -   1 )
         {
             endid 
=  ASPxGridView1.VisibleRowCount;
         }
        
else
         {
             endid 
=  (ASPxGridView1.PageIndex  +   1 *  ASPxGridView1.SettingsPager.PageSize;
         }

         CheckBox cbHead 
=  (CheckBox)sender;
        
if  (cbHead.Checked  ==   true )
         {
             CheckBox cb;
            
for  ( int  i  =  startid; i  <  endid; i ++ )
             {
                 cb 
=  (CheckBox)ASPxGridView1.FindRowCellTemplateControl(i, (GridViewDataColumn)ASPxGridView1.Columns[ " SelectAll " ],  " CheckBox1 " );
                 cb.Checked 
=   true ;
             }
         }
        
else
         {
             CheckBox cb;
            
for  ( int  i  =  startid; i  <  endid; i ++ )
             {
                 cb 
=  (CheckBox)ASPxGridView1.FindRowCellTemplateControl(i, (GridViewDataColumn)ASPxGridView1.Columns[ " SelectAll " ],  " CheckBox1 " );
                 cb.Checked 
=   false ;
             }
         }
     }
}

 

aspx文件

 

代码
<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " Default.aspx.cs "  Inherits = " 结合checkbox_Default "   %>

<% @ Register assembly = " DevExpress.Web.ASPxGridView.v7.3, Version=7.3.7.0, Culture=neutral, PublicKeyToken=6d2d307aee1dfbe0 "  namespace = " DevExpress.Web.ASPxGridView "  tagprefix = " dxwgv "   %>
<% @ Register assembly = " DevExpress.Web.ASPxEditors.v7.3, Version=7.3.7.0, Culture=neutral, PublicKeyToken=6d2d307aee1dfbe0 "  namespace = " DevExpress.Web.ASPxEditors "  tagprefix = " dxe "   %>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< html  xmlns ="http://www.w3.org/1999/xhtml" >
< head  runat ="server" >
    
< title > Untitled Page </ title >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
< div >
    
        
< dxwgv:ASPxGridView  ID ="ASPxGridView1"  runat ="server"  
             AutoGenerateColumns
="False"  DataSourceID ="SqlDataSource1"  
             KeyFieldName
="ProductID" >
            
< Columns >
                
< dxwgv:GridViewDataTextColumn  FieldName ="ProductID"  ReadOnly ="True"  
                     VisibleIndex
="0" >
                    
< EditFormSettings  Visible ="False"   />
                
</ dxwgv:GridViewDataTextColumn >
                
< dxwgv:GridViewDataTextColumn  FieldName ="ProductName"  VisibleIndex ="1" >
                
</ dxwgv:GridViewDataTextColumn >
                
< dxwgv:GridViewDataCheckColumn  FieldName ="Discontinued"  VisibleIndex ="2" >
                
</ dxwgv:GridViewDataCheckColumn >
                
< dxwgv:GridViewDataTextColumn  FieldName ="SelectAll"  VisibleIndex ="3" >
                    
< DataItemTemplate >
                        
< asp:CheckBox  ID ="CheckBox1"  runat ="server"  
                             Checked
='<%#  Eval("Discontinued") % > ' />
                    
</ DataItemTemplate >
                    
< HeaderTemplate >
                        
< asp:CheckBox  ID ="selectAll"  runat ="server"  AutoPostBack ="True"  
                             oncheckedchanged
="selectAll_CheckedChanged"   />
                         SelectAll
                    
</ HeaderTemplate >
                
</ dxwgv:GridViewDataTextColumn >
            
</ Columns >
        
</ dxwgv:ASPxGridView >
    
    
</ div >
    
< asp:SqlDataSource  ID ="SqlDataSource1"  runat ="server"  
         ConnectionString
="<%$ ConnectionStrings:NorthwindConnectionString %>"  
         SelectCommand
="SELECT [ProductID], [ProductName], [Discontinued] FROM [Products] ORDER BY [ProductID]" >
    
</ asp:SqlDataSource >
    
</ form >
</ body >
</ html >

 

 

数据库有的是Nothwind表是Products

 

你可能感兴趣的:(checkbox)