7、声明几个私有变量
/// <summary>
/// 用于存每组的全选复选框ID
/// </summary>
private
string _checkAllIDString;
/// <summary>
/// 用于存每的项复选框ID
/// </summary>
private
string _checkItemIDString;
/// <summary>
/// 每行有一个组的所有项复选框
/// </summary>
private Dictionary<
int,
string> _checkItemIDDictionary =
new Dictionary<
int,
string>();











8、重写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);
}


































































































9、重写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
添加这个控件到工具箱里,然后拖拽到webform上,在模板列的头模板处添加一个复选框,在模板列的项模板处添加一个复选框,设置控件的CheckboxAlls属性即可。CheckboxAllID是模板列全选复选框ID;CheckboxItemID是模板列项复选框ID。
ObjData.cs












/// <summary>
/// OjbData 的摘要说明
/// </summary>




























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>
<!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>

OK
[×××]
转载于:https://blog.51cto.com/webabcd/345469