cool hit counter ASP. Several types of paging in NET_Intefrankly

ASP. Several types of paging in NET


Copyright: This is an original post by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/huyuyang6688/article/details/39644255

1, through the DataGrid control to achieve paging

ASP. The DataGrid control in NET comes with a paging function. After binding the DataGrid's data source, you need to make some settings for the DataGrid control.

Left-click the small arrow in the upper-right corner of the control → Properties Builder.

Select the [Pagination] tab on the left

settle upon【 Allow paging】;【 Page Size】 Indicates how many items of data are displayed on a page; Navigation buttons for【 location】 have a roof over one's head、 the end of a period of time、 Three options for top and bottom models,【 mode】 With page number harmony“ preceding page、 Next Page Button”, If page mode is selected,【 Numeric buttons】 Indicates the maximum number of buttons to be displayed, If another model is chosen,“ preceding page”、“ next page” Can be edited to other text。

The following goes to the code to add and bind a data source to the DataGrid control.

For convenience, first write a function BindToGridView() that queries the data and binds it to the DataGrid.

private void BindToDataGrid()
    {
SqlConnection conn = DB. CreateConnection();          // Define the database connection object
         conn. Open();                                         // Open database connection
        string cmdText = "select * from T_Employee";        // Query all information from the employee table
         SqlCommand cmd = new SqlCommand(cmdText, conn);      // Define the command object
         SqlDataAdapter sda = new SqlDataAdapter(cmd);        // Define the adapter object
         DataTable dt = new DataTable();                      //define DataTable table dt
         sda. Fill(dt);                                        //Populate the DataTable table dt
         DataGrid1.DataSource = dt;                           // set DataGrid1's data source to dt
         DataGrid1.DataBind();                                //DataGrid Binding Data Source
    }

At this point the effect of pagination will be.

But when you click on the next page, it doesn't jump to the next page. This is because the control's PageIndexChanged event is triggered when you click on it, so you need to rebind it when you click on.

protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
This. DataGrid1.CurrentPageIndex = e.NewPageIndex;        // Jump to the specified new page
         BindToDataGrid();                                        // Rebind the control
    }

2. AdoptionSystem.Web.UI.WebControls.PagedDataSource Implementing paging

PageDataSource class encapsulates the properties related to paging, through the PageDataSource class, you can make Repeater and DataList for paging display, and Repeater and DataList also than the DataGrid display form diversification.

Define a static variable curPage, pageSize to indicate the current page and page size, respectively.

  private static int curPage=1;      //Display the first page by default
     private static int pageSize=3;     // default display of three data per page

For convenience, first write a function DataBindToRepeter() that paginates and binds data based on the current page, page size.

private void DataBindToRepeter()
    {
         SqlConnection conn = DB. CreateConnection();                              // Define the database connection object
         conn. Open();                                                             // Open database connection
        string cmdText = "select * from People";                                // Query all information from the employee table
         SqlCommand cmd = new SqlCommand(cmdText, conn);                          // Define the command object
SqlDataAdapter sda = new SqlDataAdapter(cmd);                            // Define the adapter object
         DataTable dt = new DataTable();                                          //define DataTable table dt
         sda. Fill(dt);                                                            //Populate the DataTable table dt

        int curPage = int.Parse(this.Label2.Text);
        System.Web.UI.WebControls.PagedDataSource pds = new PagedDataSource();  // instantiatedPageDataSource kindpds
         pds. DataSource = dt. DefaultView;                                         //Get the filtered view of dt
         pds. AllowPaging = true;                                                  // Allow paging
         pds. PageSize = 3;                                                        // Page size 3
         pds. CurrentPageIndex = curPage - 1;                                      // Current page number, starting from zero
       
         Repeater1.DataSource = pds;                                              // Set the instance ps of PageDataSource as the data source of the Repeter control
         Repeater1.DataBind();                                                    // Binding data sources
    }

Here manually add two buttons for previous page (btnPre) and next page (btnNext), and each time the button is clicked, let the value of curPage subtract one or add one respectively according to the event and then rebind.

protected void btnPre_Click(object sender, EventArgs e)
    {
         curPage=curPage-1;             //Page forward
         DataBindToRepeter();           // Rebinding
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
curPage=curPage+1;             //Page backwards
         DataBindToRepeter();           // Rebinding
    }

The principle of PageDataSource paging: PageDataSource is equivalent to extracting the data first, and then based on the Page Size harmony Current page to be displayedPerform paging and display. Since all the data needs to be extracted first, the performance of this method is not very reasonable when the amount of data to be displayed is relatively large.

3、Paging by stored procedure

Paging is achieved through a stored procedure that extracts only the data from the database to be displayed on that page, based on the conditions, then it involves if there are a total of 100 data in the database, how to take out the 50th to 60th data from the database to. This is when I thought of the true and false pagination and how to extract several consecutive entries of data in the middle of the table as taught in the Brother Brisket video.

Define the following variables in the program pageSize (how many records are displayed per page), curPage (how many pages are currently on). With these it is possible to calculate the data in the database that needs to be displayed from the first (beginPos) to the second (endPos).

The relationship between these variables is beginPos = (curPage - 1) × pageSize + 1

endPos = curPage × pageSIze

The core code of the procedure is.

CREATE PROCEDURE PRO_Paging
     --From the first data to be displayed
    @beginPos int,  
     --Displays until the end of the first piece of data
    @endPos int
AS
BEGIN
	SET NOCOUNT ON;
 	--Here a temptb table with a rowNumber column added to sort each piece of data
    with temptb as(           
    select ROW_NUMBER() over(order by pID) as rowNumber,* from People) 
     --Query the data from the @beginPos bar to the @endPos bar
    select * from temptb where rowNumber between @beginPos and @endPos 
END
GO

The advantages of this approach are, Specifies the number of pages to be displayed harmony After the size of each page, Simply extract the page of data to be displayed from the database, No need to extract large amounts of data all together, It's also what the brisket video talks about“ true pagination”, And the previous method, the paging method that takes out all the data, is“ false paging”。


Recommended>>
1、An introductory guide to deep learning a must for beginners
2、Talking about Tencent Cloud TStack on Cloud Oracle
3、Jingdong Thinking QuestionHow to buy a bottle of Coke for 001 an immodest question
4、web3j development java or android ethereum smart contract quick start
5、Java implementation of image merge method with horizontal and vertical merge support

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号