The beginning and end of the paging problem of using ListView multi-layer binding in ASP. NET

Source: Internet
Author: User

I started a website some time ago and encountered a new problem when using ListView and DataPager. First, describe the requirements of the page: There are two levels of categories, one category. Below there is a subclass, which corresponds to the product under the subclass, and then the category (which are specified) in a page) sub-classes and products are displayed by structure. The products must be displayed by page. Now, the interaction of the entire page is only on the page of the product. The main category and sub-category are only the display function on the server side. Therefore, I naturally thought of using ListView for layer-by-layer binding, add the DataPager control on the product layer for paging.

DTO in the background has also been defined, including categories, sub-categories, and products:

                       ID { ;                    IEnumerable<CategoryDTO> Categorys { ;                                  IEnumerable<ProductDTO> Products { ;                          ID { ;               }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

LV_Products.DataSource = ProductTypeList;
LV_Products.DataBind ();

Write it in Page_PreRender and try it in IIS. Well, the data is displayed normally, and the page has been divided, but I have nothing to do with other pages. The page flashed, the elements of the page are those on the first page. After troubleshooting, I think it is a data binding problem (BINDING IN THE PreRender will cause a data update every time, and my LV_Item is dynamically generated, dataPager is also dynamic, which will generate a brand new page instead of generating a page flip based on the previous PageIndex), So we changed the code bound to the background to be written! In IsPost, after reading it, it can be displayed on pages and clicked on pages. It seems that a jump has occurred. Unfortunately, the data cannot be normally bound and needs to be clicked twice to jump normally.

Think about ASP. NET page loading does not understand, so I carefully read its initialization process and think it is similar to Page_PreRender. I use ListView. the PreRender event should also achieve the same effect, so you need to cancel the LV_Item binding, and manually bind its data source in the background (LV_Products binding is placed! In IsPostBack ). When the page is loaded for the first time, all LV_Item data is bound. When the page is returned, only the corresponding LV_Item data source is rebound. The front-end code of LV_Item is changed:

 

If no, the data source binding is triggered through the ListView paging change event. The binding must occur in its PreRender event:

  LV_PageChanged(sender +=

Now I understand. When loading the page for the first time, all ListView will automatically trigger the OnPagePropertiesChanged event, so all ListView will load the PreRender event handler function for Data Binding. When you click back in the page, only one ListView will trigger the OnPagePropertiesChanged event, and only one ListView will load the PreRender event handler function. Therefore, only one LV_Item will occur when the source is rebound.

All right, LV_OnPreRender, as the rebinding function of LV_Item, requires information of specific categories and sub-categories to bind the product list to ListView, currently, I only have one LV_PageChanged file and cannot obtain the list information of which sub-class is the current ListView category. What should I do?

I had to find another way. Let's look at the client code generated by LV_Item:

What is found? There are naming rules! CPH_main is the parent version name I used, LV_Products is my main class ListView, and then ctl100, It is my small class ListView (not named, automatically generated), and then the following 0, there are 0th categories! Another ctl100 is my ajax UpdataPanel (not in this article, this is used in actual projects), followed by 0, 1, 2, 3... is actually a small class! Well, after testing, this is completely correct, as long as you have not changed the ClientIDMode. In the background code, we can use ListView. UniqueID to obtain a value similar to this type, but there is only one difference. The function of LV_OnPreRender is as follows:

       Regex _productRegex =  Regex(            LV_OnPreRender(           lv = sender                                        matche = _productRegex.Match(lv.UniqueID,                                  categoryID =              typeID =             lv.DataSource =                               }

At this point, paging is complete, which is very efficient. Testing on IIS is very fast. There are several hundred things. If you want to see the final result, you canClick meOpen the image link.

 

 

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.