Your shopping cart is empty!
Repeater can be understood by an example. If we store the menu categories in a table called “Sub_Category” in SQL Server, so that if to ever need to add one, I just add it to the table and it will appear on the menu. There are two fields in the table: Sub_Category_ID, and Sub_Category_Text.
Step 1 — Create the Page and Insert the Repeater Control
The Repeater control allows you to create templates to define the layout of its content. The templates are:
Here is a part of the Web Form (subcategories.aspx) that contains the Repeater:
The Repeater has a name of “catlist“. It uses the HeaderTemplate to print out the Areas image. It then uses the ItemTemplate to display a Hyperlink control that has our data in it. We’ll come back to this in Step Two.
Step 2 — Get the Data
Now let’s look at the data retrieval. Here is the Page_Load event in the Code Behind file.
The first five lines open a database connection and retrieve the contents of the Sub_Category table. The last two lines bind our Repeater control to the DataReader. Now, let’s look again at the ItemTemplate section:
Once the DataBind method of the Repeater control is called, ASP.NET will loop through the DataReader and populate the Repeater with the data we specify. The Databinder.Eval method uses reflection to parse and evaluate a data-binding expression against an object at run time, in this case the object is our Repeater. So this line of code:
will render the contents of the "Sub_Category_ID" field for each row in the DataReader.