In this section, I'll show you how to get more functionality from iterative databound controls such as Repeaters, DataLists, and DataGrids. With the extra functionality comes a richer user interface, which will ultimately yield a better application. Applications that have smooth user interfaces almost always work better because users more easily grasp their functionality.
To pull this enhancement off, server controls such as Labels, CheckBoxes, and DropDownLists can be embedded into iterative databound controls. Getting access to the embedded controls is easy, thus making the iterative databound controls very flexible.
The scenario is this: You've convinced your boss to let you develop the next Web application with ASP.NET. He's reluctant, but one of the selling points is the iterative databound controls, including Repeaters, DataLists, and DataGrids. These three databound controls differ from other databound controls such as the DropDownList or ListBox because they're iterative. Iterative controls loop through a data source to which they're bound and apply HTML templates to each row of data.
OK; so three weeks have passed, and your boss comes by to see the work in progress. One of the pages has a Repeater object that's been bound to a recordset that was retrieved from the database. The application looks great, and he's really impressed with the small amount of code it took to get the result. He asks for a change, though. You need to be able to select each row with a checkbox so that users can select any number of items for processing.
You get busy and scour the Repeater documentation. There must be something about checkboxes. But hours of research are fruitless, and you throw up your hands in despair. Maybe a Repeater wasn't the object type you should have used. You take a look at the DataList and DataGrid objects, hoping to find the solution, but no such luck. No checkboxes are built into the iterative databound controls.
Hold on, though; there is hope. Server controls such as CheckBoxes, Labels, and TextBoxes can all be embedded within ASP.NET iterative databound controls. With the right approach, the Repeater in the already-developed ASP.NET application can be easily changed to give users the capability to select the items in the list. This section talks about embedding server controls in iterative databound controls, and shows some example programs that'll help you understand.
ASP.NET iterative databound controls give you an easy way to display lists of data from a data source. They also offer flexibility because you can add server controls such as Labels and CheckBoxes to the declarative constructs, such as ItemTemplates, that go in the .aspx file. The section is broken into three parts: a Repeater demo, a DataList demo, and a DataGrid demo.
For each demo, I'll talk about the declaration of the objects that reside in the .aspx file. This declaration includes things such as ItemTemplates and Columns.
For each demo, I'll also go through the code that's behind it. We'll rely heavily on the FindControl() method. This method obtains object references to the server controls that we want to access.
All code snippets are presented in C# and VB. The complete demo project can be downloaded as either a C# or a VB project.