7、声明几个私有变量
/// <summary>
InBlock.gif                 /// 用于存每组的全选复选框ID
InBlock.gif                 /// </summary>
InBlock.gif                 private string _checkAllIDString;
InBlock.gif                 /// <summary>
InBlock.gif                 /// 用于存每的项复选框ID
InBlock.gif                 /// </summary>
InBlock.gif                 private string _checkItemIDString;
InBlock.gif                 /// <summary>
InBlock.gif                 /// 每行有一个组的所有项复选框
InBlock.gif                 /// </summary>
InBlock.gif                 private Dictionary< int, string> _checkItemIDDictionary = new Dictionary< int, string>();
 
8、重写OnRowDataBound以给我们声明的那些私有变量赋值。
/// <summary>
InBlock.gif                 /// OnRowDataBound
InBlock.gif                 /// </summary>
InBlock.gif                 /// <param name="e"></param>
InBlock.gif                 protected override void OnRowDataBound(GridViewRowEventArgs e)
InBlock.gif                {
InBlock.gif                         if (e.Row.RowType == DataControlRowType.DataRow)
InBlock.gif                        {
InBlock.gif                                 // GridViewRow的每个TableCell
InBlock.gif                                 for ( int i = 0; i < e.Row.Cells.Count; i++)
InBlock.gif                                {
InBlock.gif                                         // TableCell里的每个Control
InBlock.gif                                         for ( int j = 0; j < e.Row.Cells[i].Controls.Count; j++)
InBlock.gif                                        {
InBlock.gif                                                 if (e.Row.Cells[i].Controls[j] is CheckBox)
InBlock.gif                                                {
InBlock.gif                                                        CheckBox chk = (CheckBox)e.Row.Cells[i].Controls[j];
InBlock.gif
InBlock.gif                                                         // 判断该CheckBox是否属于全选CheckBox    
InBlock.gif                                                         bool isCheckboxAll = false;
InBlock.gif                                                         foreach (CheckboxAll ca in CheckboxAlls)
InBlock.gif                                                        {
InBlock.gif                                                                 if (chk.NamingContainer.ClientID + "_" + ca.CheckboxItemID == chk.ClientID)
InBlock.gif                                                                {
InBlock.gif                                                                        isCheckboxAll = true;
InBlock.gif                                                                         break;
InBlock.gif                                                                }
InBlock.gif                                                        }
InBlock.gif
InBlock.gif                                                         // 给该CheckBox增加客户端代码
InBlock.gif                                                         if (isCheckboxAll)
InBlock.gif                                                        {
InBlock.gif                                                                 // 给Control增加一个客户端onclick
InBlock.gif                                                                chk.Attributes.Add( "onclick", "yy_ClickCheckItem()");
InBlock.gif                                                                 // 给_checkItemIDDictionary赋值
InBlock.gif                                                                 if (_checkItemIDDictionary.Count == 0 || !_checkItemIDDictionary.ContainsKey(i))
InBlock.gif                                                                {
InBlock.gif                                                                        _checkItemIDDictionary.Add(i, chk.ClientID);
InBlock.gif                                                                }
InBlock.gif                                                                 else
InBlock.gif                                                                {
InBlock.gif                                                                         string s;
InBlock.gif                                                                        _checkItemIDDictionary.TryGetValue(i, out s);
InBlock.gif                                                                        _checkItemIDDictionary.Remove(i);
InBlock.gif                                                                        _checkItemIDDictionary.Add(i, s + this.ItemSeparator + chk.ClientID);
InBlock.gif                                                                }
InBlock.gif
InBlock.gif                                                                 break;
InBlock.gif                                                        }
InBlock.gif                                                }
InBlock.gif                                        }
InBlock.gif                                }
InBlock.gif                        }
InBlock.gif                         else if (e.Row.RowType == DataControlRowType.Header)
InBlock.gif                        {
InBlock.gif                                 // GridViewRow的每个TableCell
InBlock.gif                                 for ( int i = 0; i < e.Row.Cells.Count; i++)
InBlock.gif                                {
InBlock.gif                                         // TableCell里的每个Control
InBlock.gif                                         for ( int j = 0; j < e.Row.Cells[i].Controls.Count; j++)
InBlock.gif                                        {
InBlock.gif                                                 if (e.Row.Cells[i].Controls[j] is CheckBox)
InBlock.gif                                                {
InBlock.gif                                                        CheckBox chk = (CheckBox)e.Row.Cells[i].Controls[j];
InBlock.gif
InBlock.gif                                                         // 判断该CheckBox是否属于全选CheckBox    
InBlock.gif                                                         bool isCheckboxAll = false;
InBlock.gif                                                         foreach (CheckboxAll ca in CheckboxAlls)
InBlock.gif                                                        {
InBlock.gif                                                                 if (chk.NamingContainer.ClientID + "_" + ca.CheckboxAllID == chk.ClientID)
InBlock.gif                                                                {
InBlock.gif                                                                        isCheckboxAll = true;
InBlock.gif                                                                         break;
InBlock.gif                                                                }
InBlock.gif                                                        }
InBlock.gif
InBlock.gif                                                         // 给该CheckBox增加客户端代码
InBlock.gif                                                         if (isCheckboxAll)
InBlock.gif                                                        {
InBlock.gif                                                                 // 给Control增加一个客户端onclick
InBlock.gif                                                                chk.Attributes.Add( "onclick", "yy_ClickCheckAll(this)");
InBlock.gif                                                                 // 给_checkAllIDString赋值
InBlock.gif                                                                 if (String.IsNullOrEmpty( this._checkAllIDString))
InBlock.gif                                                                {
InBlock.gif                                                                         this._checkAllIDString += chk.ClientID;
InBlock.gif                                                                }
InBlock.gif                                                                 else
InBlock.gif                                                                {
InBlock.gif                                                                         this._checkAllIDString += this.GroupSeparator + chk.ClientID;
InBlock.gif                                                                }
InBlock.gif                                                                 break;
InBlock.gif                                                        }
InBlock.gif                                                }
InBlock.gif                                        }
InBlock.gif                                }
InBlock.gif                        }
InBlock.gif
InBlock.gif                         base.OnRowDataBound(e);
InBlock.gif                }
 
9、重写GridView的OnPreRender方法,以实现每行复选框的全选与取消全选的功能。
/// <summary>
InBlock.gif                 /// OnPreRender
InBlock.gif                 /// </summary>
InBlock.gif                 /// <param name="e"></param>
InBlock.gif                 protected override void OnPreRender(EventArgs e)
InBlock.gif                {
InBlock.gif                         base.OnPreRender(e);
InBlock.gif
InBlock.gif                         // CheckboxAlls里有对象则注册一些完成实现全选功能的客户端脚本
InBlock.gif                         if (CheckboxAlls.Count > 0)
InBlock.gif                        {
InBlock.gif                                 // 注册实现 每行复选框的全选与取消全选 功能的JavaScript
InBlock.gif                                 if (!Page.ClientScript.IsClientScriptBlockRegistered( "JsCheckAll"))
InBlock.gif                                {
InBlock.gif                                        Page.ClientScript.RegisterClientScriptBlock(
InBlock.gif                                                 this.GetType(),
InBlock.gif                                                 "JsCheckAll", JavaScriptConstant.jsCheckAll.Replace( "[$AllName$]", this.HiddenCheckboxAllID).Replace( "[$ItemName$]", this.HiddenCheckboxItemID).Replace( "[$GroupSeparator$]", this.GroupSeparator.ToString()).Replace( "[$ItemSeparator$]", this.ItemSeparator.ToString())
InBlock.gif                                                );
InBlock.gif                                }
InBlock.gif
InBlock.gif                                 // 给_checkItemIDString赋值
InBlock.gif                                _checkItemIDString = "";
InBlock.gif                                 foreach (KeyValuePair< int, string> kvp in _checkItemIDDictionary)
InBlock.gif                                {
InBlock.gif                                        _checkItemIDString += this.GroupSeparator + kvp.Value;
InBlock.gif                                }
InBlock.gif                                 if (_checkItemIDString.StartsWith( this.GroupSeparator.ToString()))
InBlock.gif                                {
InBlock.gif                                        _checkItemIDString = _checkItemIDString.Remove(0, 1);
InBlock.gif                                }
InBlock.gif
InBlock.gif                                 // 注册实现 每行复选框的全选与取消全选 功能的两个隐藏字段
InBlock.gif                                 // 有的时候回发后没有重新绑定GridView,就会造成_checkAllIDString和_checkItemIDString为空
InBlock.gif                                 // 所以把这两个值存到ViewSate中
InBlock.gif                                 if (!String.IsNullOrEmpty(_checkAllIDString) && !String.IsNullOrEmpty(_checkItemIDString))
InBlock.gif                                {
InBlock.gif                                        ViewState[ this.HiddenCheckboxAllID] = _checkAllIDString;
InBlock.gif                                        ViewState[ this.HiddenCheckboxItemID] = _checkItemIDString;
InBlock.gif                                }
InBlock.gif                                 if (ViewState[ this.HiddenCheckboxAllID] != null && ViewState[ this.HiddenCheckboxItemID] != null)
InBlock.gif                                {
InBlock.gif                                        Page.ClientScript.RegisterHiddenField( this.HiddenCheckboxAllID, ViewState[ this.HiddenCheckboxAllID].ToString());
InBlock.gif                                        Page.ClientScript.RegisterHiddenField( this.HiddenCheckboxItemID, ViewState[ this.HiddenCheckboxItemID].ToString());
InBlock.gif                                }
InBlock.gif                        }
InBlock.gif                }
 
控件使用
添加这个控件到工具箱里,然后拖拽到webform上,在模板列的头模板处添加一个复选框,在模板列的项模板处添加一个复选框,设置控件的CheckboxAlls属性即可。CheckboxAllID是模板列全选复选框ID;CheckboxItemID是模板列项复选框ID。
ObjData.cs
InBlock.gif using System;
InBlock.gif using System.Data;
InBlock.gif using System.Configuration;
InBlock.gif using System.Web;
InBlock.gif using System.Web.Security;
InBlock.gif using System.Web.UI;
InBlock.gif using System.Web.UI.WebControls;
InBlock.gif using System.Web.UI.WebControls.WebParts;
InBlock.gif using System.Web.UI.HtmlControls;
InBlock.gif
InBlock.gif using System.ComponentModel;
InBlock.gif
/// <summary>
/// OjbData 的摘要说明
/// </summary>
InBlock.gif public class OjbData
InBlock.gif{
InBlock.gif         public OjbData()
InBlock.gif        {
InBlock.gif                 //
InBlock.gif                 // TODO: 在此处添加构造函数逻辑
InBlock.gif                 //
InBlock.gif        }
InBlock.gif
InBlock.gif        [DataObjectMethod(DataObjectMethodType.Select, true)]
InBlock.gif         public DataTable Select()
InBlock.gif        {
InBlock.gif                DataTable dt = new DataTable();
InBlock.gif                dt.Columns.Add( "no", typeof( string));
InBlock.gif                dt.Columns.Add( "name", typeof( string));
InBlock.gif
InBlock.gif                 for ( int i = 0; i < 30; i++)
InBlock.gif                {
InBlock.gif                        DataRow dr = dt.NewRow();
InBlock.gif                        dr[0] = "no" + i.ToString().PadLeft(2, '0');
InBlock.gif                        dr[1] = "name" + i.ToString().PadLeft(2, '0');
InBlock.gif
InBlock.gif                        dt.Rows.Add(dr);
InBlock.gif                }
InBlock.gif
InBlock.gif                 return dt;
InBlock.gif        }
InBlock.gif}
 
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>SmartGridView测试</title>
</head>
<body>
        <form id="form1" runat="server">
                <div>
                         
                        <yyc:SmartGridView ID="SmartGridView1" runat="server" AutoGenerateColumns="False"
                                DataSourceID="ObjectDataSource1" Width="100%">
                                <Columns>
                                        <asp:TemplateField>
                                                <headertemplate>
                                                        <asp:checkbox id="checkall" runat="server" />
                                                </headertemplate>
                                                <itemtemplate>
                                                        <asp:checkbox id="checkitem" runat="server" />
                                                </itemtemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                                <itemtemplate>
                                                        abc
                                                </itemtemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                                <headertemplate>
                                                        <asp:checkbox id="checkall2" runat="server" />
                                                </headertemplate>
                                                <itemtemplate>
                                                        <asp:checkbox id="checkitem2" runat="server" />
                                                </itemtemplate>
                                        </asp:TemplateField>
                                </Columns>
                                <CheckboxAlls>
                                        <yyc:CheckboxAll CheckboxAllID="checkall" CheckboxItemID="checkitem" />
                                        <yyc:CheckboxAll CheckboxAllID="checkall2" CheckboxItemID="checkitem2" />
                                </CheckboxAlls>
                                <SortTip SortAscImage="~/Images/asc.gif" SortDescImage="~/Images/desc.gif" />
                        </yyc:SmartGridView>
                        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Select"
                                TypeName="OjbData"></asp:ObjectDataSource>
                </div>
        </form>
</body>
</html>
 
InBlock.gif /*测试版的实现 结束*/