GridView 分頁後,SelectedIndexChanging 資料綁定錯誤

摘要:GridView 分頁後,SelectedIndexChanging 資料綁定錯誤

今天又抓到一隻前輩留下來的蟲,錯誤發生在當GridView點選分頁後,不管點選哪個選項都只會出現第一頁的分頁資料,看了一下,原來是算錯index了broken heart!!!

原本的程式碼:

   1:  protected void GridViewRC_PageIndexChanging(object sender, GridViewPageEventArgs e)
   2:        {
   3:             List rcs = (List) Session["EditRCs"];
   4:             this.GridViewRC.PageIndex = e.NewPageIndex;
   5:             bindRCGridView(rcs);
   6:         }
   7:   
   8:  protected void GridViewRC_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
   9:         {
  10:             List rcs = (List)Session["EditRCs"];
  11:             BSMDSS.BFSM.SMEC.CNR.Web.WRRC.RC rc = rcs[e.NewSelectedIndex];
  12:             Session.Add("EditRC", rc);
  13:             Response.Redirect("~/RC/FRMEditRC.aspx");
  14:         }

修改後的程式碼:

 
   1:  protected void GridViewRC_PageIndexChanging(object sender, GridViewPageEventArgs e)
   2:         {
   3:             List rcs = (List) Session["EditRCs"];
   4:             this.GridViewRC.PageIndex = e.NewPageIndex;
   5:             bindRCGridView(rcs);
   6:         }
   7:   
   8:  protected void GridViewRC_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
   9:         {
  10:             List rcs = (List)Session["EditRCs"];
  11:             BSMDSS.BFSM.SMEC.CNR.Web.WRRC.RC rc = rcs[e.NewSelectedIndex+GridViewRC.PageIndex*GridViewRC.PageSize];
  12:             Session.Add("EditRC", rc);
  13:             Response.Redirect("~/RC/FRMEditRC.aspx");
  14:         }