Implement real Ajax pagination using the ObjectDataSource control in ASP. NET

Source: Internet
Author: User

Tutorial ASP. the newly added ListView control in NET 3.5 is a great control for page data binding and interface layout. It is stored in ASP. NET 2.0 GridView has made a lot of improvements, users can control more elements during use, and more flexibility during development, I personally think that ListView is more convenient than DataGrid and GirdView when used. The ListView control does not have the paging function, but ASP. with the newly added DataPager control in. NET, we can easily set pagination for the data in the ListView. This requires almost no developers to write a line of code and place the ListView control on the page, set the layout and DataSource, add a DataPager control, set its PagedControlID attribute to the ID of the ListView, and set the number of data entries to be displayed on each page in PageSize, then set the pagination style in Fields (of course, you do not need to worry about the style, ASP.. NET will define the paging UI according to the built-in style), run the Web program, you will see a ListView page that supports pagination, the whole process is very simple. In ASP. in NET 3.5, Microsoft separated the data binding control from the paging control, so that users can set pages anywhere on the page and define different paging styles according to their own needs, at the same time, the paging control can control any data binding control, which is specified through the PagedControlID attribute. Basically, for paging operations, developers can do whatever they like. How to Use the ListView control and DataPager control is not the focus of this article. Interested readers can check Microsoft's MSDN. I think it should be much more detailed than I have said.

Let's talk about the principle of data paging. When developing Web applications for data binding pages, we often encounter a large amount of data. To prevent the page from becoming too large to load data slowly, the data to be displayed on a page is displayed by page. When a user accesses a page, the page-based function is used to view the data on different pages. This is a good solution, in addition, almost all program designers and developers will show data on the page by page. This is no problem! The problem lies in the paging method.

In general, the simplest way is to read data on all pages to the cache media at a time (this media is generally the memory of the server), and then display only one page of data at a time. This method is easy to implement, and ASP. in the past, almost all data binding controls that support paging were used in this way, so that many ASP. NET Beginners use this method to develop paging Data Binding pages, and are not aware of any problems. Yes. Generally, the simplest and most effective method used in program development will not cause any problems. Moreover, standard controls provided by Microsoft do this. What are the problems? For some small Web applications, this is indeed no problem, because it involves a small amount of data, even if we read all the data into the memory, it is only a few megabytes at best, A little more than 10 megabytes, dozens of megabytes. If the data is plain text (generally, the data stored in the database is text information), dozens of megabytes of data are already thousands of records, the current hardware conditions on the server are good, and the memory is above the G level. It is basically impossible to process this data. However, if the number of records in a table in the database Reaches hundreds of millions, and some fields store file data (that is, binary data ), in this way, it is not an ideal practice to read all the data into the memory at one time. In this case, you need to use the "real paging" method to read the data.

In most cases, we still need to use the "real paging" method to obtain data. Given the starting position (or index of the page) of each page record, and then given the number of data entries and total number of records displayed per page, we want to retrieve only the data on the current page each time. Each time when a user is paging, some data is taken from the database and bound to the page based on these conditions, which can greatly reduce the overhead of the server and a large amount of data is not a problem. This method seems ideal. However, we will find that even if we use the "real paging" method to retrieve data by page, we will still encounter problems. Imagine that in today's Ajax-like Web world, websites that use Ajax to improve user experience are emerging one after another. If you happen to have a paging Data Binding page that uses Ajax, then the problem will occur. Because the Ajax user experience is partial page refresh, on the page data binding page, When you click the page button, the page will update the page data at a fast speed, this experience is quite good for users, but greedy users may want to try clicking the paging button frequently, or even crazy users click the paging button, at this time, your application may encounter a script error because it needs to frequently retrieve paging data from the database and cannot update the data on the page, the user experience is that the page paging function is abnormal and the program crashes.

The combination of "true paging" and "false paging" can effectively solve the problem mentioned above. I call the first data paging method as "false paging", and the second data paging method as "Real paging ". The combination of the two paging methods means that data on n pages is read to the cache at a time, you can determine whether to directly retrieve data from the cache or load data from the database to the cache based on your needs. After all, loading data from the cache is much more efficient. In this way, when you click the paging button, as long as the data exists in the cache, you can load the data very quickly. If the cache expires or the data you want to obtain exceeds the cache, load the new n-page data from the database to the cache. Of course, you can use the synchronous update cache process in Ajax to restrict the UI operations in this process.

In fact, there are a lot of details involved in paging. It is far from enough to give a detailed explanation of all the issues, here I just want to introduce you to a type. NET Ajax method. To make it easier to use Ajax, I directly used the Ajax controls in the ajaxToolkit package provided by Microsoft in Visual Studio. These controls are generally quite useful, the usage of these controls is not introduced here.

Before writing this article, I also read a lot of materials. In fact, when developing data binding pages, we usually use the "real paging" method to process data by PAGE, ASP. the DataPager control in NET 3.5 is a good control for data paging. Some people put the defect that the data binding control provided by Microsoft does not support data "Real paging" on it, I think this is an grievance against it. DataPager is only responsible for paging operations. Regardless of the data source, it is more important to handle paging UI and interaction with users. So how to handle the data source? How does the data binding control know how many pages my data source has been divided into? What page of data is my current data?

These problems once made me very upset, and I tried to use them. NET PagedDataSource object paging data, but later found that this object also needs to read all the data to the memory at a time to support paging. To put it bluntly, it is also a "false paging" data source object, which is no different from DataGrid and GridView. Remember from. NET 2.0, Microsoft provides a series of data source controls (such as SqlDataSource, XmlDataSource, LinqDataSource, and so on) to simplify data source designation for data binding controls, in fact, I think these controls have no great value in addition to simplifying the code, and sometimes they will damage the structure of the program itself. I have always opposed to using these controls directly on the page (of course, it is very convenient to use these controls in programs used for demonstration ). However, I accidentally saw the ObjectDataSource control in the Visual Studio toolbox while studying Ajax's real paging. At first I thought it should be the base control of those DataSource controls, later, I checked the information to know that this control is the only control in all DataSource controls that supports the "real paging" operation. It can achieve the data paging function by setting a few simple attributes, next I will introduce how to use this control.

  • 5 pages in total:
  • Previous Page
  • 1
  • 2
  • 3
  • 4
  • 5
  • Next Page

Related Article

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.