求助:
   怎么实现记住上一页勾选的记录;;;默认选中!!

解决方案 »

  1.   


            #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;
                    }
                }
            }    }
      

  2.   

    第一次数据绑定给列表的时候要初始化
    //初始化所有数据的CheckBox状态为false
    CheckBoxState = InitializeUsersDic(collections);