当前位置:网站首页>An improvement of the code in the article "Walkthrough: creating paged data access using web form pages" in MSDN

An improvement of the code in the article "Walkthrough: creating paged data access using web form pages" in MSDN

2022-06-21 07:41:00 Shifeng 13

If you install it MSDN, Learning to use .NET Make a website , You may encounter such a sample document :

Visual Basic and Visual C# Concept

rehearse : Use Web Form pages create paged data access

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/vbcon/html/vbwlkwalkthroughdisplayingdatainlistboxesonwebformspage.htm

This article describes how to create a data list with custom paging , The feature is that only the corresponding number of pieces of data are extracted from the database each time , This method is very good for the list display of large data tables , It is better than another scheme that takes out all the data at once and then caches the pages .

When I adopted this scheme, I found several small BUG, as follows :

  1. stay “ Go to the next page ” In the program code of ,

    private void btnNext_Click(object sender, System.EventArgs e)
    {
       // Get the page number of the page most recently displayed
       CurrentPage = (int)(ViewState["CurrentPage"]);
       CurrentPage++;
       // Gets the id on current page
       string lastid = DataGrid1.Items[9].Cells[0].Text;
       cmdNext.Parameters["@customerid"].Value = lastid;
       FillGrid(cmdNext);
    }
    The scarlet letter should read “string lastid = DataGrid1.Items[DataGrid1.PageSize-1].Cells[0].Text;”, Otherwise, when the number of (PageSize) Not the default 10 Stripe time , May cause errors .

  2. In addition to “ Show data in Grid ” In this part , You will see that some codes judge when the number of entries taken on this page is less than PageSize when , That is, when it is regarded as the last page of the data table , Set up “ The next page ” Button failure , You should also add the judgment that the current page is 0 Or less than 0 when , That is, when it is regarded as the first page of the data sheet , Set up “ The previous page ” Button failure code , as follows :

       if (DataGrid1.Items.Count < DataGrid1.PageSize)
       {
        btnNext.Enabled = false;
       }
       if (CurrentPage <= 0)
       {
        btnPrevious.Enabled = false;
       }

  3. Steps in 2 At the same time , stay btnNext_Click This sentence should also be added to the method “this.btnPrevious.Enabled = true;”, So that after page change “ The previous page ” Button activated first , Then follow the steps 2 To judge whether the setting button is invalid .
  4. Add : A new problem has just been found in the inspection , When the number of entries on the last page is just the number of entries on the page ,“ The next page ” The button is still active , Click to make an error , Because there is no corresponding record to display . Therefore, the following supplementary amendments are made :( Pay attention to the red marking )

       if(dr.HasRows==true)
       {
        DataGrid1.DataSource = dr;
        DataGrid1.DataBind();
        dr.Close();
        sqlConnection1.Close();
        ViewState["CurrentPage"] = CurrentPage;
        ViewState[CurrentPage.ToString()] = DataGrid1.Items[0].Cells[0].Text;
        if (DataGrid1.Items.Count < DataGrid1.PageSize)
        {
         btnNext.Enabled = false;
        }
        if (CurrentPage <= 0)
        {
         btnPrevious.Enabled = false;
        }
       }
       else
       {
        btnNext.Enabled = false;
       }

thus , After my test, such a paging list is more perfect , You can adapt to different page numbers .

原网站

版权声明
本文为[Shifeng 13]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206210739232352.html