Using the ASP.NET Repeater Control

Posted by: Mark Kendall , on 3/5/2011, in Category ASP.NET
Views: 143846
Abstract: In this article, we will explore how to use the ASP.NET Repeater Control and interact with a DropDownList in the code behind. I will try to show much more than the obvious that just might save you a couple of search clicks, the next time you need to use the repeater control for ASP.NET

Programming in today’s world requires you to have many talents in many different areas, if you are going to be able to solve even the smallest business requirement. But we cannot know everything about anything, so that’s why we a programmers favorite friend is Google. You know I’m right- how much time you spend on Google?

The problem is there is “sooo” much bad information out there that you have to be a genius to figure out what information is going to help you, and what information to ultimately ignore. And you have to click forever to get a comprehensive overview of any one subject- for example the ASP.net repeater control- sure there is lots of info out there, but is mostly fragmented information on the BASICS of the control- not the information that is required to solve most business problems.

 

So in this article I will try to show much more than the obvious that just might save a couple of clicks the next time you need to use the repeater control for ASP.NET. Let’s begin:

ASP.NET Repeater Control

To start with, you should use a repeater control if you want full control on how you want the presentation of the data. Here is what Microsoft says “A data-bound list control that allows custom layout by repeating a specified template for each item displayed in the list.

For this article I will use this markup to get my point across and note that I have added a dropdown box (one of the topics I found few words on Google) to the repeater. Later in the article I will show you how to interact with the dropdown in the code behind.

Most ASP.NET developers would have no problem creating the look and feel for their repeater, but interacting with the elements in the code behind can be challenging: Here is the code behind for this repeater:

Note: The code is in VB.NET but can be converted to C# using this VB.NET to C# Free Conversion tool

The key to interacting with the form is to have a way to find what row you are on when you’re trying to update a database or do some other kind of action. For example, on the SelectedIndexChanged of the dropdown box, here is how you find the selected value:

InputPub_SelectedIndexChanged  (See above)
Dim myList As DropDownList = CType(sender, DropDownList)

You can cast the sender to a DropDownList and then read the selected value

Response.Write(myList.SelectedValue & " " & tb.Text)

In this example you can either update the database with a button or you can use the SelectedIndexChanged, as shown above.

Note: Before running the application, use the script.sql file in the source code to create a ‘Sample’ table and a stored procedure in the Northwind database.

So from this example, you can get instructive text to do the following with a repeater:

  • Display your data using Templates and HTML to get the best look possible
  • Use a Dropdown to toggle different actions
  • Use a button to update a database
  • Find specific elements within the repeater to facilitate database updates
  • Call a Stored procedure from the code-behind
  • Have a javascript alert pop up on the button click to warn before a deletion
  • Bind repeater to database or custom class- See attached source code project. The code is in VB.NET but can be converted to C# using this VB.NET to C# Free Conversion tool

Summary

So that’s the repeater control and many of the pieces that makes the use of it much more than just functional. If you were to study this article and play with the attached source code before you run off to Google for all those fragments, you might be one step ahead of your competition.

I have included several other ways to interact with a repeater control in the attached source code. See y’all next time- and happy coding.

The entire source code of this article can be downloaded over here

Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Mark Kendall is a Software Engineer working in the Oil Industry in Dallas,Texas. He has been a programmer/developer for past 15 years specializing in .NET/C# development. Currently, he is a Sharepoint/Silverlight developer specializing in BCS Services. He has published a book on Dotnetnuke, and written several articles on many .net subjects. Apart from his passion for making money writing software to improve business, Mark enjoys ranting-and-raving about current events and taking long runs on the beach. He blogs at kendallsoft and can be reached at Kendallsoft[attherate]hotmail[dot]com


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by Carter Smeaton on Saturday, March 5, 2011 7:49 PM
Thanks Mark, something similar around the Listview would be a great time saver too
Comment posted by Sujoy SS on Monday, March 14, 2011 1:41 AM
nice article...keep posting
Comment posted by krishna on Monday, January 9, 2012 5:03 AM
nice posteded
Comment posted by nick grey on Monday, February 20, 2012 1:06 AM
Hi,
could you tell how am i going to apply the repeater on this scenario
if I have radgrid and when you select a record in the grid, the detail of that record will be displayed in the label.  The datasource will be taken from stored procedure (linq)


==========================
|Id|LOgin Name|Description
==========================
|01|JohnDoe   |John Doe


Label Id : 01
Label Name : John Doe

Thanks
Comment posted by dharmveer kumar on Monday, May 28, 2012 7:57 AM
nice posteded
Comment posted by gjgj on Monday, June 11, 2012 8:28 AM
fgg
Comment posted by gjgj on Monday, June 11, 2012 8:28 AM
fgg
Comment posted by dsadas on Tuesday, July 3, 2012 8:02 AM
asdsdasdasd
Comment posted by hahaha on Tuesday, July 3, 2012 8:02 AM
aavu na hoy
Comment posted by Ritesh on Wednesday, July 11, 2012 3:41 AM
good article!!!
Comment posted by asdadasd on Tuesday, September 25, 2012 1:49 AM
dklsjfj
Comment posted by cvb on Wednesday, October 3, 2012 2:10 AM
cv
Comment posted by P.RAJASEKAR on Thursday, December 20, 2012 12:24 AM
how to select a row from repeater,the repeater contains table columns and one radio button.
how to select a row using that radio button,please help me
Comment posted by khan on Wednesday, December 26, 2012 5:35 AM
dsafaf
Comment posted by vhjhj on Saturday, January 19, 2013 12:59 AM
dddddddddddddddddddddddddddddhhhdfhgfhgdhgdhgdgdg
Comment posted by Puru on Friday, March 1, 2013 6:00 AM
jljdlasjdljlk
Comment posted by sri on Monday, March 25, 2013 12:02 AM
nice
Comment posted by hhhhhhhhhh on Tuesday, April 2, 2013 7:00 AM
how to make this post your comment in website to don't repeat the same data
Comment posted by hhhhhhhhhh on Tuesday, April 2, 2013 7:04 AM
how to make this post your comment in website to don't repeat the same data
Comment posted by rohit on Tuesday, June 4, 2013 3:43 AM
gud thanks
Comment posted by Arun on Thursday, June 13, 2013 6:37 AM
Even this comment section is an example of Repeater control. If I am not wrong.

Repeater controls can also be used to display files in a folder using the controls header and footer section also. You can refer to this article.
http://www.encodedna.com/2013/02/repeater-control-in-aspdotnet.htm
Comment posted by Arun on Thursday, June 13, 2013 6:39 AM
Even this comment section is an example of Repeater control. If I am not wrong.

Repeater controls can also be used to display files in a folder using the controls header and footer section also. You can refer to this article.
http://www.encodedna.com/2013/02/repeater-control-in-aspdotnet.htm
Comment posted by dasd on Monday, July 15, 2013 11:33 PM
dadasdas
Comment posted by Azhar Hussain Khan on Friday, July 26, 2013 5:47 AM
this is a example of repeater controls...
Comment posted by Naveen on Saturday, August 17, 2013 12:12 AM
gud thanks
Comment posted by Naveen on Saturday, August 17, 2013 1:02 AM
gud thanks
Comment posted by Suresh on Friday, January 10, 2014 5:19 AM
Hi nice
Comment posted by ABC on Tuesday, April 15, 2014 5:49 AM
A to z :P
Comment posted by .net learner on Tuesday, July 1, 2014 2:21 AM
how to use paging in repeater control

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel