Asp.Net北大青鳥總結(四)-使用GridView實現真假分頁

來源:互聯網
上載者:User

標籤:net   北大青鳥   nbsp   name   UI   hang   password   pos   tar   

    這段時間看完了asp.net視頻。可是感覺到自己的學習好像沒有鞏固好,於是又在圖書館裡借了幾本關於asp.net的書感覺真的非常好自己大概對於asp.net可以實現主要的小Demo。可是我知道僅僅有真正的使用才可以有所收穫,並且自己的認識度還是要進一步的學習。在這一部分的學習中自己也算是對於分頁有了一個主要的瞭解了吧,也用它做出來的幾個主要的Demo。那麼接下來我們來看一下這個控制項的用於真假分頁的一些用法。

   一.什麼是真假分頁

    1.假分頁:

    假分頁儘管在介面上實現了分頁的,可是他並沒有實現分頁。每一次點擊頁數的時候都要從資料庫中講全部的資料再又一次查一遍,也就是說這樣每次都從資料庫中查詢一次那麼就會影響他查詢速度。所以假分頁儘管是實現了介面上的分頁可是還是沒有實現真正的分頁。

   2.真分頁:

    那麼我們知道假設想要真正的實現分頁,就是每次僅僅是查詢出想要頁面的資料。這樣就能夠降低每次查詢的資料的量並且還能夠提高查詢的效率,真正的實現了翻一頁查一頁。

二.基本實現

   1.假分頁

   我們視頻裡的視頻比較曆史悠久所以當我們學到這裡的時候控制項已經改了名字了,可是還是換湯不換藥。還是要經過這麼幾個步驟才幹夠。1>要在gridview中改變他的AllowPaging屬性改為true,然後將PageSize設為你想分頁的數目。然後接下來就是要將控制項綁定資料庫

protected void Page_Load(object sender, EventArgs e)          {              //頁面第一次載入              if (!Page.IsPostBack )              {                  //綁定資料                  GridView1.DataSource = MyData();                  GridView1.DataBind();              }          }          protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)          {              GridView1.PageIndex = e.NewPageIndex;              DataTable dt = MyData();                        GridView1.DataSource = dt;              GridView1.DataBind();            }                private static DataTable  MyData()          {              SqlConnection con = new SqlConnection("server=.;database=northwind;uid=sa;password=gss123");              con.Open();              string strCmd = "select * from comment";              SqlCommand cmd = new SqlCommand(strCmd,con );              SqlDataReader sdr = cmd.ExecuteReader();              DataTable dt=new DataTable ();              dt.Load(sdr );              con.Close();              return dt;              }      }  

以上就是假分頁的資料繫結。

2.真分頁

  真分頁但是要花一點功夫的,真分頁是須要aspnetpaper這個第三方控制項的,須要下載他的DLL檔案然後在拖動至工具箱的然後在使用它,他就是一個翻頁的工具

 protected void Page_Load(object sender, EventArgs e)          {              if (!Page.IsPostBack )              {                  //視窗打卡時,起始資料編號為0,終止資料編號為每頁的資訊條數                  int intStartIndex = ANP.PageSize * 0;                  int intEndIndex = ANP.PageSize * 1;                <span style="color:#ff0000;"><strong>  ANP.RecordCount = MyAllData().Rows.Count;</strong></span>                  //綁定資料                  GridView1.DataSource = MyPartData(intStartIndex, intEndIndex);                  GridView1.DataBind();              }          }          /// <summary>          /// 改變了頁數的資料顯示          /// </summary>          /// <param name="sender"></param>          /// <param name="e"></param>          protected void ANP_PageChanged(object sender, EventArgs e)          {              //起始資料編號=每頁的資訊容量*(當前頁數-1)+1。              //例:第六頁,每頁顯示5條,第六頁的起始資料=5*5+1=26;              int intStartIndex=ANP.PageSize * (ANP.CurrentPageIndex-1)+1;              int intEndIndex = ANP.PageSize * ANP.CurrentPageIndex;              GridView1.DataSource = MyPartData(intStartIndex,intEndIndex );              GridView1.DataBind();            }          /// <summary>          /// 分頁查詢          /// </summary>          /// <param name="intStartIndex">開始的資料編號</param>          /// <param name="intEndIndex">結束的資料編號</param>          /// <returns>表格</returns>          private static DataTable MyPartData(int intStartIndex,int intEndIndex)          {              SqlConnection con = new SqlConnection("server=.;database=newssystem;uid=sa;password=123456");              con.Open();              string strCmd = "select * from comment";              SqlCommand cmd = new SqlCommand(strCmd,con );              SqlDataReader sdr = cmd.ExecuteReader();              DataTable dt=new DataTable ();              dt.Load(sdr );              con.Close();              return dt;            }          /// <summary>          /// 所有查詢          /// </summary>          /// <returns></returns>          private static DataTable MyAllData()          {              SqlConnection con = new SqlConnection("server=.;database=northwind;uid=sa;password=gss123");              con.Open();              string Cmd = "select * from comment";              SqlCommand cmd = new SqlCommand(Cmd, con);              SqlDataReader sdr = cmd.ExecuteReader();              DataTable dt = new DataTable();              dt.Load(sdr);              con.Close();              return dt;            }      }  

     經過這段學習我瞭解了關於真假分頁的事兒所以,以後還要繼續學習他的一些東西,還有我明確了有時候自己走不通的時候能夠看看別人是怎麼做的,可是不要全然的借鑒,也要有自己的體會。


Asp.Net北大青鳥總結(四)-使用GridView實現真假分頁

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.