on 28 February 18
During the development of web application with ASP.NET, you may have to come across a situation in which you need to display a section in your left bar or right bar containing a list of certain items. Data of these items must be coming from the database. For example, if you are developing a car selling website and you need to show 4 cars which have been placed for sale most recently. This situation requires the use of a specific ASP.NET control referred to as Repeater control.
Rather than having to run a loop to display database values, Repeater provides a very easy to use method to display database items in a list.
Here, an example is shown which has to display a list of most recent cars placed for sale, along with the image of the cars.
Step 1)The first thing that you need to do is to define the data source of Repeater control. We will use ObjectDataSource as the source of this control. You can create an ObjectDataSource control by dragging it from the Data category of the Toolbox.
Step 2) Drag the ObjectDataSource from the toolbox, click on the arrow symbol visible on the right side of control and click on Configure data source to define the source of data.
Step 3) This will take you to a wizard to define data source properties. At the completion of this process following code will be generated for the newly created ObjectSource control.
Step 4) After defining the Data Source object you must create the Repeater control in a similar way, by dragging it from the Data category of the Toolbox.
Step 5) Then set the Datasource property of the Repeater object to the newly created ObjectSource control. Following piece of code reflects the settings of Repeater object.
ItemTemplate attribute refers to the settings of an individual record. Here we have to display the car image and car title and clicking on any of these must take the user to the description of related car. Both of these are encapsulated into HyperLink control. ItemAddress is a function defined in the vb file. It receives car id and returns the web page on which information about the selected car may be available.
Protected Function itemAddress(ByVal val As Integer) As String Return "carDetails.aspx?CarID=" & val End Function Then there is an image control of ASP.NET. ImageUrl property of Image control is obtained with the help of imgAddress() function defined in the vb file. And then car title is displayed. The Eval function having the input of a field returns the relevant ObjectSource data.
The resultant repeater control may look like this on a web page.
Learn more about programming here.