Basics of Binding to Data Controls - The Rant

Binding sets of records to data controls is one of the most misunderstood concepts in the .NET development world. The Data controls, GridView, DataList, Repeater, now the ListView and formerly the DataGrid all have one thing in common, binding records of data to them. We can also add the FormView and DetailsView to the mix too. While this should be relatively trivial to all ASP.NET programmers I continually see posts on the ASP.NET forums about problems with the basics of binding data to these controls. I think much of it comes from reading very bad ASP.NET tutorials that show us how to do things the wrong way for a real application.

Do not get me wrong this is not the problem with the newbie programmer, but rather a fault of us who are trying to explain to these new programmers how to tackle the common task of binding records of data to these controls. In order for us to produce a short and simple tutorial we show how to drop a GridView and a SQLDataSource control on a page and be done with it. Oh sure we may take it a little beyond this and show how to use BoundFields, etc. with declarative binding techniques. But this is all too wrong. We need to be showing how to really bind data to these controls with real business objects, using the RowDataBound and ItemDataBound event handlers. We need to explain how to leverage custom Templates and event our own ITemplates. In short we need to demonstrate how to actually create production quality code!

First we need to demonstrate how to use the DataList and the Repeater controls. These controls are fundamental and display records in a controlled and repeatable format. The Repeater should be the data control of choice. The DataList should be the second and only used when there are multiple columns to display, such as a product catalog (see for an example.

But the Repeater makes me use templates, that means I have to work. Precisely and you will be compensated for it either with a paycheck or with higher revenues. Yes it takes slightly longer to create and bind a template, but in order to make the GridView format the data the way you or your client really wants it will take far longer to do and you will revert back to the same template as the Repeater anyway.

The next thing you must realize is that we have a plethora of great code generators available at our finger tips, so buy one and get going. This will help you produce this code in a factory like fashion where it is consistent and just about complete as soon as you can push the generate button.

In summary, stop using the GridView and the SQLDataSource objects. Get away from declarative databinding. Use custom templates in the Repeater and DataList controls. Leverage the power of code generators to make this process relatively easy.

I plan on backing this up as I slowly release a long series of entries on how to properly do databinding to the right data controls over the next couple of months.

Share This Article With Your Friends!