web| Create | access | pagination | Data some time ago to do the first ASP.net project, very simple. When you have a small problem with the DataGrid paging, do a reference to the MSDN example. There seems to be a problem in the actual process, which has improved:
Link: ms-help://ms. Msdnqtr.2003feb.2052/vbcon/html/vbwlkwalkthroughdisplayingdatainlistboxesonwebformspage.htm
Description: This article uses two SQL statements to use different statements when paging forward and paging backwards. After improvement, only one statement can be used. I use stored procedures in the project:
CREATE PROCEDURE Selectalluser
(
@Id int
)
As
Select Top Id,username,idcard,sex,birthday,mailaddr,getscholartime
From UserInfo
where id>= @Id
Go
The key code is as follows:
///
Read 15 records from the database that started from UserID and display
///
///
private void Showalluser (int userID)
{
Managedb managedb = new Managedb ();
int count = Managedb.getusercount (); This method gets the total number of records
if (count = = 1)
{
Response.Redirect ("Error.aspx", true);
Return
}
Count = count/this.griduser.pagesize;
SqlDataReader reader = Managedb.getalluser (UserID);
This.gridUser.DataSource = reader;
This.gridUser.DataBind ();
Reader. Close ();
viewstate["currentpage"] = currentpage;
Viewstate[currentpage.tostring ()] = This.griduser.items[0]. Cells[0]. Text;
if (CurrentPage <=0)
{
this.btnPrevious.Enabled = false;
}
if (currentpage >= count)
{
this.btnNext.Enabled = false;
}
}
Next Page button click event:
private void Btnnext_click (object sender, System.EventArgs e)
{
This.btnPrevious.Enabled = true;
currentpage = (int) (viewstate["currentpage"]);
CurrentPage + +;
The following code has a +1 operation, which is necessary to ensure that the records obtained are not duplicated.
int lastid = Convert.ToInt32 (this.griduser.items[this.griduser.pagesize-1). Cells[0]. Text) + 1;
This.showalluser (LastID);
}
Previous Page button click event
private void Btnprevious_click (object sender, System.EventArgs e)
{
This.btnNext.Enabled = true;
CurrentPage = Convert.ToInt32 (viewstate["currentpage"));
currentpage--;
if (currentpage >= 0)
{
int FirstID;
FirstID = Convert.ToInt32 (viewstate[(currentpage). ToString ()]);
This.showalluser (FirstID);
}
}
The last page Load event is added:
if (! Page.IsPostBack)
{
currentpage = 0;
This.showalluser (0);
}
This makes for a simple paging improvement. Of course we can also display the total number of pages, the current number of pages. But this paging function is not strong, can not be done directly to the first few pages. But the paging efficiency can be said to be quite high.
The entire source code can be downloaded here, note that the database is SQL server2000.
In addition my blog will be introduced recently. NET books as well as the source code download, please pay attention!