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 : On page 2 : 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. 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.
} }
Here we have fetched data using LINQ from SQL inside usrcmnts. We create a new PagedDataSource and provide the datasource as usrcmnts. PAGED data source is selected as this would help us in handling paging for this repeater control.
tbl.Rows.Add(tr); lc.DataBinding += new EventHandler(bindrptrdata); container.Controls.Add(tbl); } public void bindrptrdata(object sender, EventArgs e) { LiteralControl lc = (LiteralControl)sender; RepeaterItem container = (RepeaterItem)lc.NamingContainer; lc.Text = DataBinder.Eval(container.DataItem, "user").ToString() + " says : </BR></BR>" + DataBinder.Eval(container.DataItem, "comment").ToString(); } } Here our repeater will repeat data in a table which has a literal control in it. This literal control gets its data from bindrptrdata method.
lnk.Click += new EventHandler(handlePaging); Repeater1.Controls.Add(lnk); } } public void handlePaging(object sender, EventArgs e) { LinkButton lnk = (LinkButton)sender; int nextpage = int.Parse(lnk.Text); fetchData(nextpage * pagesize, (nextpage - 1) * pagesize); } The Math.Ceiling has been used if the datacount is not perfectly divisible by pagesize for example datacount=6 and pagesize=4. In this case we would need 2 pages. handlePaging method handles the data fetching when we click on links to change the page. This method calls fetchData with two parameters. Here we are going to see why we have used the TAKE and SKIP in linq queries.
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 !!
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: