GridView加入CheckBox实现全选,跨页CheckBox选择 求助: 怎么实现记住上一页勾选的记录;;;默认选中!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 #region 记录CheckBox状态 //声明一个Dictionary(并将其表明为ViewState属性), //其中Key值(string)代表User的ID(数据库中的主键),Value值(bool)表示该行的CheckBox是否选中 public Dictionary<string, bool> CheckBoxState { get { return (ViewState["CheckBoxState"] != null) ? (Dictionary<string, bool>)ViewState["CheckBoxState"] : null; } set { ViewState["CheckBoxState"] = value; } } public T03_OrderInfoCollection Orders { get { return (Session["Collections"] != null) ? (T03_OrderInfoCollection)(Session["Collections"]) : null; } set { Session["Collections"] = value; } } //初始化Dictionary protected Dictionary<string, bool> InitializeUsersDic(T03_OrderInfoCollection collections) { Dictionary<string, bool> currentDic = new Dictionary<string, bool>(); //将集合中的主键内容存储到Dictionary中 foreach (T03_OrderInfo collection in collections) { currentDic.Add(collection.OrderInfoNum, false); } return currentDic; } #endregion protected void gvOrderInfo_PageIndexChanging(object sender, GridViewPageEventArgs e) { //记录分页前的CheckBox状态 Pagination.RememberOldValues(CheckBoxState, this.gvOrderInfo); this.gvOrderInfo.PageIndex = e.NewPageIndex; this.gvOrderInfo.DataSource = Orders; this.gvOrderInfo.DataBind(); //给绑定完成的页面CheckBox赋值 Pagination.RePopulateValues(CheckBoxState, this.gvOrderInfo); }public class Pagination { //记录分页的前一页CheckBox的状态 public static void RememberOldValues(Dictionary<string, bool> dic, GridView gdv) { foreach (GridViewRow row in gdv.Rows) { Label lblId = row.FindControl("lblId") as Label; CheckBox currentCbx = row.FindControl("chkIsDefault") as CheckBox; string currentValue = lblId.Text; if (currentCbx.Checked && dic[currentValue] == false) { dic[currentValue] = true; } else if (!currentCbx.Checked && dic[currentValue] == true) { dic[currentValue] = false; } } } //给分页完毕的ChecxBox赋值 public static void RePopulateValues(Dictionary<string, bool> dic, GridView gdv) { foreach (GridViewRow row in gdv.Rows) { Label lblId = row.FindControl("lblId") as Label; CheckBox currentCbx = row.FindControl("chkIsDefault") as CheckBox; string currentValue = lblId.Text; if (dic[currentValue] == true) { currentCbx.Checked = true; } else { currentCbx.Checked = false; } } } } 第一次数据绑定给列表的时候要初始化//初始化所有数据的CheckBox状态为falseCheckBoxState = InitializeUsersDic(collections); 求助 Failed to create AppDomain 打开网页10分钟后弹出框并播放音乐 .net如何获取页面的浏览数次 求助水晶报表的发布 遍逆页面里所有控件,并得到其所属类型,textbox,checkbox…… asp.net中gridview中如何显示表头? 有没有同道中人在研习 PetShop 4.0啊,呵呵 框架加载面页问题 求推荐一个开源商城 关于webservice安全性的问题 DatabaseType的问题 求助!!! 用户Null 登录失败 未与信任sqlserver 连接相关联
#region 记录CheckBox状态 //声明一个Dictionary(并将其表明为ViewState属性),
//其中Key值(string)代表User的ID(数据库中的主键),Value值(bool)表示该行的CheckBox是否选中
public Dictionary<string, bool> CheckBoxState
{
get
{
return (ViewState["CheckBoxState"] != null) ? (Dictionary<string, bool>)ViewState["CheckBoxState"] : null;
}
set
{
ViewState["CheckBoxState"] = value;
}
} public T03_OrderInfoCollection Orders
{
get
{
return (Session["Collections"] != null) ? (T03_OrderInfoCollection)(Session["Collections"]) : null;
}
set
{
Session["Collections"] = value;
}
} //初始化Dictionary
protected Dictionary<string, bool> InitializeUsersDic(T03_OrderInfoCollection collections)
{
Dictionary<string, bool> currentDic = new Dictionary<string, bool>();
//将集合中的主键内容存储到Dictionary中
foreach (T03_OrderInfo collection in collections)
{
currentDic.Add(collection.OrderInfoNum, false);
}
return currentDic;
} #endregion
protected void gvOrderInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//记录分页前的CheckBox状态
Pagination.RememberOldValues(CheckBoxState, this.gvOrderInfo); this.gvOrderInfo.PageIndex = e.NewPageIndex;
this.gvOrderInfo.DataSource = Orders;
this.gvOrderInfo.DataBind(); //给绑定完成的页面CheckBox赋值
Pagination.RePopulateValues(CheckBoxState, this.gvOrderInfo);
}public class Pagination
{
//记录分页的前一页CheckBox的状态
public static void RememberOldValues(Dictionary<string, bool> dic, GridView gdv)
{
foreach (GridViewRow row in gdv.Rows)
{
Label lblId = row.FindControl("lblId") as Label;
CheckBox currentCbx = row.FindControl("chkIsDefault") as CheckBox;
string currentValue = lblId.Text;
if (currentCbx.Checked && dic[currentValue] == false)
{
dic[currentValue] = true;
}
else if (!currentCbx.Checked && dic[currentValue] == true)
{
dic[currentValue] = false;
}
}
} //给分页完毕的ChecxBox赋值
public static void RePopulateValues(Dictionary<string, bool> dic, GridView gdv)
{
foreach (GridViewRow row in gdv.Rows)
{
Label lblId = row.FindControl("lblId") as Label;
CheckBox currentCbx = row.FindControl("chkIsDefault") as CheckBox;
string currentValue = lblId.Text;
if (dic[currentValue] == true)
{
currentCbx.Checked = true;
}
else
{
currentCbx.Checked = false;
}
}
} }
//初始化所有数据的CheckBox状态为false
CheckBoxState = InitializeUsersDic(collections);