.CS code
Private void page_load (object sender, system.eventargs e) {// Place user code here to initialize page Pagesize = 10; SQL = "Select * from products Order by all all_time desc"; if (! page.ispostback) {/ / total number of records is calculated RecordCount = CalculateRecord (); // calculate the total number of pages @ rounding PageCount = RecordCount / PageSize; if (RecordCount% PageSize> 0) PageCount = PageCount 1; lblPageCount.Text = PageCount.ToString (); Lblrecordcount.text = RecordCount.toString (); viewState ["pagecount"] = PageCount; currentpage = 0; viewState ["pageIndex"] = 0; // Bind Listbind ();
}} Public int CalculateRecord () {int intCount; string strCount = "select count (*) as co from products"; SqlConnection Con = new SqlConnection (data.constr); SqlCommand addCommand = new SqlCommand (strCount, Con); addCommand. Connection.open (); SqlDataReader DR; DR = addcommand.executeReader (); if ()) {INTCOUNT = INT32.PARSE (DR ["Co"]. Tostring ());} else {Intcount = 0 } Dr.close (); Return IntCount;
Icollection createsource () {
int StartIndex; // set imported from a final address StartIndex = CurrentPage * PageSize; string strSel = "select * from products"; SqlConnection Con = new SqlConnection (data.constr); DataSet ds = new DataSet (); SqlDataAdapter MyAdapter = new SqlDataAdapter (strSel, Con); MyAdapter.Fill (ds, StartIndex, PageSize, "products"); return ds.Tables [ "products"] DefaultView;.} public void ListBind () {dl1.DataSource = CreateSource (); dl1.DataBind (); lbnNextPage.Enabled = true; lbnPrevPage.Enabled = true; if (PageCount == 0) {lblCurrentPage.Text = "0"; lbnNextPage.Enabled = false; lbnPrevPage.Enabled = false;} else {if (CurrentPage == (PageCount-1)) lbnNextPage.Enabled = false; if (CurrentPage == 0) lbnPrevPage.Enabled = false; lblCurrentPage.Text = (CurrentPage 1) .ToString ();}} public void Page_OnClick (Object Sender, CommandEventArgs E) {currentpage = (int) ViewState ["pageindex"]; pageCount = (int) ViewState ["pagecount"]; string cmd = e.commandName; // judge CMD to determine the page switch SWWW ITCH (CMD) {CASE "Next": IF (CURRENTPAGE <(PageCount-1)) CurrentPage ; Break; Case "prev": if (CurrentPage> 0) CurrentPage -; Break; Case "first": currentpage = 0; Break; Case "last": currentpage = pagecount-1; break;} viewstate ["pageindex"] = currentpage;
Listbind ();