Search code examples
kentico

CMS Repeater Select top N row++


I need a load more button to control the repeater shown content.

Here is my steps in the back end after load more button was clicked:

  1. Repeater bind data (Full data/All rows).
  2. Keep the maximum items count (static) from the repeater.
  3. Set total row to show (static) + 1 and initialize the value to select top N row as the limit.
  4. Repeater bind data again (The amount to show only).
  5. Check if repeater items count is less than maximum items count, if not then hide the load more button.

Suppose these steps can give me the expected output?

//Declaration
public static int max = 0;
public static int totalShow = 0;

//SetupControl()
if(!IsPostBack){
  rptItems.ClassName = "Blog";
  rptItems.Path = "/Shared/%"
  rptItems.DataBind();
  max = rptItems.Items.Count();
}

//This part is put under a new function
totalShow += 1;
rptItems.SelectTopN = totalShow;
rptItems.DataBind();
lbnLoadMore.Visible = rptItems.Items.Count() < max;

Besides, I'm confusing about the functions as shown below:

enter image description here

enter image description here

Both are from the class CMSRepeater, what's the different? Which one should I use in order to set the limits?


Solution

  • Using static members is definitely not a good approach. Their values would be shared by all users of the application. There are better ways of storing user-specific data:

    • session (server-side)
    • JS (client-side) and passing them to the server via query string or hidden field

    Regarding TopN and SelectTopN, they do the same thing. It's probably because of backward compatibility.

    From the algorithmical point of view, there's no need to bind data multiple times nor to make more than one round-trip to the database. You just need to initialize the datasource/repeater with correct values.

    I'd recommend you reading the following articles to get some inspiration: