Improvement of "Web Forms Creating Patement Data Access" on MSDN

zhaozj2021-02-16  57

I did the first ASP.NET project for some time. It is very simple. When you encounter a small problem during the DataGrid page, you will do one for the MSDN example. In the actual process, it seems that it seems a bit problem, improved:

Link: MS-Help: //ms.msdnqtr.2003feb.2052/vbcon/html/vbwlkwalkthroughdisplayingdatainlistboxesonwebformspage.htm

Description: This article uses two SQL statements to use different statements when you turn forward and backwards. Improve only one statement will be used. I use stored procedures in the project: create procedure selectalluser (@ID int) as select top 15 id, username, idcard, sex, birthday, mailaddr, getscholartime from userinfo where id> = @ ID Go

The key code is as follows: ///

/// Read 15 records starting from UserID from the database and display ///// Private Void ShowAllUser (int UserID)

{

Managedb managedb = new managementb ();

INT count = managedb.getusercount ();

// This method gets the total record number if (count == -1)

{

Response.Redirect ("Error.aspx", True;

Return;

}

Count = count / this.griduser.pagesize;

SqlDataReader Reader = Managedb.GetAlluser (userID);

THIS.GRIDUSER.DASOURCE = Reader;

THIS.GRIDUSER.DATABIND ();

Reader.Close ();

ViewState ["CurrentPage"] = CurrentPage;

ViewState [currentpage.tostring ()] = this.griduser.Items [0] .Cells [0] .TEXT;

CurrentPage <= 0)

{

this.btnprevious.enabled = false;

}

IF (CurrentPage> = count)

{

this.btnnext.enabled = false;

}

}

Next button click event: private void btnNext_Click (object sender, System.EventArgs e) {this.btnPrevious.Enabled = true; CurrentPage = (int) (ViewState [ "CurrentPage"]); CurrentPage ; // below The code has a 1 operation, this is necessary, so that the record has not been repeated. INT Lastid = Convert.Toint32 (this.griduser.Items [this.griduser.pagesize-1] .Cells [0] .text) 1; this.ShowAllUser (LastID);

Previous 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 (first);}} Last is added to the Page Load event:

IF (! page.ispostback) {currentpage = 0; this.showalluser (0);}

This improves a simple paging. Of course, we can also display the total number of pages, the current number of current pages. But this paging function is not strong, you can't do it directly to the first few pages. But this paging efficiency can be said to be quite high.

The whole source code can be downloaded here, pay attention to the database is SQL Server2000. In addition, my blog recently launched .NET books and source code downloads, please pay attention!

转载请注明原文地址:https://www.9cbs.com/read-17468.html

New Post(0)