上接扩展GridView控件(4) - 联动复选框(复选框的全选和取消全选)

 
七、声明几个私有变量
/// <summary>
                 /// 用于存每组的全选复选框ID
                 /// </summary>
                 private string _checkAllIDString;
                 /// <summary>
                 /// 用于存每的项复选框ID
                 /// </summary>
                 private string _checkItemIDString;
                 /// <summary>
                 /// 每行有一个组的全部项复选框
                 /// </summary>
                 private Dictionary< int, string> _checkItemIDDictionary = new Dictionary< int, string>();
 
八、重写OnRowDataBound以给咱们声明的那些私有变量赋值。
/// <summary>
                 /// OnRowDataBound
                 /// </summary>
                 /// <param name="e"></param>
                 protected override void OnRowDataBound(GridViewRowEventArgs e)
                {
                         if (e.Row.RowType == DataControlRowType.DataRow)
                        {
                                 // GridViewRow的每一个TableCell
                                 for ( int i = 0; i < e.Row.Cells.Count; i++)
                                {
                                         // TableCell里的每一个Control
                                         for ( int j = 0; j < e.Row.Cells[i].Controls.Count; j++)
                                        {
                                                 if (e.Row.Cells[i].Controls[j] is CheckBox)
                                                {
                                                        CheckBox chk = (CheckBox)e.Row.Cells[i].Controls[j];

                                                         // 判断该CheckBox是否属于全选CheckBox    
                                                         bool isCheckboxAll = false;
                                                         foreach (CheckboxAll ca in CheckboxAlls)
                                                        {
                                                                 if (chk.NamingContainer.ClientID + "_" + ca.CheckboxItemID == chk.ClientID)
                                                                {
                                                                        isCheckboxAll = true;
                                                                         break;
                                                                }
                                                        }

                                                         // 给该CheckBox增长客户端代码
                                                         if (isCheckboxAll)
                                                        {
                                                                 // 给Control增长一个客户端onclick
                                                                chk.Attributes.Add( "onclick", "yy_ClickCheckItem()");
                                                                 // 给_checkItemIDDictionary赋值
                                                                 if (_checkItemIDDictionary.Count == 0 || !_checkItemIDDictionary.ContainsKey(i))
                                                                {
                                                                        _checkItemIDDictionary.Add(i, chk.ClientID);
                                                                }
                                                                 else
                                                                {
                                                                         string s;
                                                                        _checkItemIDDictionary.TryGetValue(i, out s);
                                                                        _checkItemIDDictionary.Remove(i);
                                                                        _checkItemIDDictionary.Add(i, s + this.ItemSeparator + chk.ClientID);
                                                                }

                                                                 break;
                                                        }
                                                }
                                        }
                                }
                        }
                         else if (e.Row.RowType == DataControlRowType.Header)
                        {
                                 // GridViewRow的每一个TableCell
                                 for ( int i = 0; i < e.Row.Cells.Count; i++)
                                {
                                         // TableCell里的每一个Control
                                         for ( int j = 0; j < e.Row.Cells[i].Controls.Count; j++)
                                        {
                                                 if (e.Row.Cells[i].Controls[j] is CheckBox)
                                                {
                                                        CheckBox chk = (CheckBox)e.Row.Cells[i].Controls[j];

                                                         // 判断该CheckBox是否属于全选CheckBox    
                                                         bool isCheckboxAll = false;
                                                         foreach (CheckboxAll ca in CheckboxAlls)
                                                        {
                                                                 if (chk.NamingContainer.ClientID + "_" + ca.CheckboxAllID == chk.ClientID)
                                                                {
                                                                        isCheckboxAll = true;
                                                                         break;
                                                                }
                                                        }

                                                         // 给该CheckBox增长客户端代码
                                                         if (isCheckboxAll)
                                                        {
                                                                 // 给Control增长一个客户端onclick
                                                                chk.Attributes.Add( "onclick", "yy_ClickCheckAll(this)");
                                                                 // 给_checkAllIDString赋值
                                                                 if (String.IsNullOrEmpty( this._checkAllIDString))
                                                                {
                                                                         this._checkAllIDString += chk.ClientID;
                                                                }
                                                                 else
                                                                {
                                                                         this._checkAllIDString += this.GroupSeparator + chk.ClientID;
                                                                }
                                                                 break;
                                                        }
                                                }
                                        }
                                }
                        }

                         base.OnRowDataBound(e);
                }
 
九、重写GridView的OnPreRender方法,以实现每行复选框的全选与取消全选的功能。
/// <summary>
                 /// OnPreRender
                 /// </summary>
                 /// <param name="e"></param>
                 protected override void OnPreRender(EventArgs e)
                {
                         base.OnPreRender(e);

                         // CheckboxAlls里有对象则注册一些完成实现全选功能的客户端脚本
                         if (CheckboxAlls.Count > 0)
                        {
                                 // 注册实现 每行复选框的全选与取消全选 功能的JavaScript
                                 if (!Page.ClientScript.IsClientScriptBlockRegistered( "JsCheckAll"))
                                {
                                        Page.ClientScript.RegisterClientScriptBlock(
                                                 this.GetType(),
                                                 "JsCheckAll", JavaScriptConstant.jsCheckAll.Replace( "[$AllName$]", this.HiddenCheckboxAllID).Replace( "[$ItemName$]", this.HiddenCheckboxItemID).Replace( "[$GroupSeparator$]", this.GroupSeparator.ToString()).Replace( "[$ItemSeparator$]", this.ItemSeparator.ToString())
                                                );
                                }

                                 // 给_checkItemIDString赋值
                                _checkItemIDString = "";
                                 foreach (KeyValuePair< int, string> kvp in _checkItemIDDictionary)
                                {
                                        _checkItemIDString += this.GroupSeparator + kvp.Value;
                                }
                                 if (_checkItemIDString.StartsWith( this.GroupSeparator.ToString()))
                                {
                                        _checkItemIDString = _checkItemIDString.Remove(0, 1);
                                }

                                 // 注册实现 每行复选框的全选与取消全选 功能的两个隐藏字段
                                 // 有的时候回发后没有从新绑定GridView,就会形成_checkAllIDString和_checkItemIDString为空
                                 // 因此把这两个值存到ViewSate中
                                 if (!String.IsNullOrEmpty(_checkAllIDString) && !String.IsNullOrEmpty(_checkItemIDString))
                                {
                                        ViewState[ this.HiddenCheckboxAllID] = _checkAllIDString;
                                        ViewState[ this.HiddenCheckboxItemID] = _checkItemIDString;
                                }
                                 if (ViewState[ this.HiddenCheckboxAllID] != null && ViewState[ this.HiddenCheckboxItemID] != null)
                                {
                                        Page.ClientScript.RegisterHiddenField( this.HiddenCheckboxAllID, ViewState[ this.HiddenCheckboxAllID].ToString());
                                        Page.ClientScript.RegisterHiddenField( this.HiddenCheckboxItemID, ViewState[ this.HiddenCheckboxItemID].ToString());
                                }
                        }
                }
 
控件使用
添加这个控件到工具箱里,而后拖拽到webform上,在模板列的头模板处添加一个复选框,在模板列的项模板处添加一个复选框,设置控件的CheckboxAlls属性便可。CheckboxAllID是模板列全选复选框ID;CheckboxItemID是模板列项复选框ID。
ObjData.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.ComponentModel;

/// <summary>
/// OjbData 的摘要说明
/// </summary>
public class OjbData
{
         public OjbData()
        {
                 //
                 // TODO: 在此处添加构造函数逻辑
                 //
        }

        [DataObjectMethod(DataObjectMethodType.Select, true)]
         public DataTable Select()
        {
                DataTable dt = new DataTable();
                dt.Columns.Add( "no", typeof( string));
                dt.Columns.Add( "name", typeof( string));

                 for ( int i = 0; i < 30; i++)
                {
                        DataRow dr = dt.NewRow();
                        dr[0] = "no" + i.ToString().PadLeft(2, '0');
                        dr[1] = "name" + i.ToString().PadLeft(2, '0');

                        dt.Rows.Add(dr);
                }

                 return dt;
        }
}
 
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>
 
/*测试版的实现 结束*/
 
相关文章
相关标签/搜索