A repeater control in ASP.NET is a databound control. Repeater controls are used in scenarios where we need to display data that changes, however the template in which data is displayed remains the same and repeats several times.

For example, a repeater control can be used in case of displaying questions for an exam(where template is similar for all questions ie question and answer options), or a survey or user comments etc. In this article we are going to write a repeater control which fetches user comments data and displays in a repeater control. We are also going to take care of Paging for the repeater.

Note : This article assumes that the reader knows how to fetch data from SQL using LINQ. If not, please go through this article http://www.c-sharpcorner.com/UploadFile/scottlysle/L2SinCS06022008035847AM/L2SinCS.aspx. Getting started section will give an introduction on how to use LINQ with SQL

At the end of the article we are going to have a control which looks like this. There are six comments and the pagesize is set to 2

On page 1 :

paging in LINQ
On page 2 :

LINQ
For this article we have a simple sql table called "Comments" which has two columns "User" and "Comment".

Create a new Web site using visual studio. Drag and drop the "Repeater" control from the DATA section of the toolbox on the designer page.

paging
We are going to fetch data from the SQL using LINQ and bind it to the repeater control using the Datasource property.

The repeater control repeats the data in a particular template. This template can be programmed as per our requirement. by writing a class which implements ITemplate interface. Point # 3 explains this in detail.

This logic has been used for paging. On the link button click we are sending the appropriate parameters to display the data related to that page number.

For Example : If the page size is 2 and the total comment data is 6 (ie 3 pages in all). Suppose we are on page number 1. Now when I click on 2 ie next page of the paging control, we should be displayed 3rd and 4th row right ?

So we call the function as

fetchData(nextpage * pagesize, (nextpage - 1) * pagesize); where nextpage=2 here
this will call function as : fetchData(2*2,(2-1)*2) = fetchData(4,2)

Therefore the fetchData function has TAKE=4 and SKIP=2. So it displays 3rd and 4th row for the 2nd page where Pagesize 2. Similary, you can work out for page size 3 also.

The completed c# code file is attached here. Have a look though it.

Cheers !!