MyPage is a personalized page based on your interests.The page is customized to help you to find content that matters you the most.


I'm not curious

Tutorial : Repeater Control in ASP.NET

Published on 19 December 12
383
0
0

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.
Tutorial : Repeater Control in ASP.NET - Image 1
(ObjectDataSource Box)
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.
Tutorial : Repeater Control in ASP.NET - Image 2
(Toolbox)
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.
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataBy" TypeName="DataSet1TableAdapters.CarsTableAdapter" DeleteMethod="Delete"> <DeleteParameters> <asp:Parameter Name="Original_CarID" Type="String" /> </DeleteParameters> </asp:ObjectDataSource>
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.
Tutorial : Repeater Control in ASP.NET - Image 3
(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.
<asp:Repeater id="Repeater1" runat="server" DataSourceID="objectdatasource1"> <ItemTemplate> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl=' <%# itemAddress(Eval("CarID")) %>' ForeColor= #2a4f5e Target=_parent> <asp:Image ID="Image1" runat="server" ImageUrl=' <%# imgAddress(Eval("CarID")) %>' Width=50 Height=50 /> <asp:Label ID="Label1" runat="server" Text='<%# Eval("Cartitle") %>'></asp:Label></asp:HyperLink><br /> </ItemTemplate> </asp:Repeater>
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.
Tutorial : Repeater Control in ASP.NET - Image 4
(Repeater Control)
-----
Learn more about programming here.



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.

Tutorial : Repeater Control in ASP.NET - Image 1

(ObjectDataSource Box)

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.

Tutorial : Repeater Control in ASP.NET - Image 2

(Toolbox)

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.

Tutorial : Repeater Control in ASP.NET - Image 3

(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.

Tutorial : Repeater Control in ASP.NET - Image 4

(Repeater Control)

-----

Learn more about programming here.

This review is listed under Development & Implementations and Data & Information Management Community

Post a Comment

Please notify me the replies via email.

Important:
  • We hope the conversations that take place on MyTechLogy.com will be constructive and thought-provoking.
  • To ensure the quality of the discussion, our moderators may review/edit the comments for clarity and relevance.
  • Comments that are promotional, mean-spirited, or off-topic may be deleted per the moderators' judgment.
You may also be interested in
Awards & Accolades for MyTechLogy
Winner of
REDHERRING
Top 100 Asia
Finalist at SiTF Awards 2014 under the category Best Social & Community Product
Finalist at HR Vendor of the Year 2015 Awards under the category Best Learning Management System
Finalist at HR Vendor of the Year 2015 Awards under the category Best Talent Management Software
Hidden Image Url

Back to Top