您的位置:首页 > 职场人生

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

2007-01-08 08:54 609 查看
上接>>

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


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>


/*测试版的实现 结束*/

OK
[源码下载]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐