Using the ASP.NET Repeater Control
Posted by: Mark Kendall
in Category ASP.NET
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
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
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
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!