Improvements on the WEB Forms page creation Paging data access above MSDN

Source: Internet
Author: User
Tags count tostring
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!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.