Databound Controls in ASP.NET 2.0

posted on 10/22/05 at 11:26:58 pm by Joel Ross

As I've posted about previously, I'm working through the?process updating our server control for Tourney Logic. Well, we made a pretty big design decision. Since the bracket is much easier to configure, we've decided to add databinding - we only have one main collection now, and it's only one level deep, so it makes perfect sense for us to do that. Before, we had two collections, and one was two levels deep, so while databinding was possible, it wasn't really viable to do so.

Having made the decision, I moved forward with what needed to be done. When I started, I had no idea! I'd never built a databound control before, so I started doing some digging. It turns out that ASP.NET 2.0 offers much better support for data bound controls than in 1.1. There's a whole new hierarchy of base classes that you can plug into where you want to. For us, we used the combination of the composite and data bound control - the DataBoundCompositeControl - as our base. It gives the same benefits of the new CompositeControl base class, but adds in the ability to databind.

Once you start from there, adding in databinding is pretty straight forward. It's a general recommendation, but it was especially useful here - if you want to learn how databinding should be implemented, use Reflector and see how Microsoft did it. Anyway, what we wanted was to let people create a collection of teams anyway they want, and then bind that collection to the TBC. Now, the collection could be any collection - not just?a true collection, but also a DataTable, etc.?

Anyway, databinding is actually pretty straight forward. First, you create properties to hold the names of the properties in the collection. That's confusing! To simplify things, let's think of the DropDownList. You can set two properties besides the datasource when databinding: DataTextField and DataValueField. Those are the names of the properties on your collection to use for the text and value field, respectively. So, for the Tourney Bracket Control, we actually have four fields you can bind to: CompetitorId, CompetitorName, SeedNumber, and NavigateUrl:

Bracket1.DataCompetitorNameField = "TeamName";

Obviously, we have three others for the other properties, but it's pretty straight forward. Here's where things get interesting.?Instead of your ordinary CreateChildControls() method, you get a new CreateChildControls method that takes a couple of parameters. One of them is the IEnumerable datasource, and?from this, you can get all of the data out of the datasource. To do this, you get an enumerator:

IEnumerator e = dataSource.GetEnumerator();

Then, you loop for each record, and get a reference to the data object:

object data = e.Current;

Then, to get the data out of the object, you use the DataBinder class:

competitor.CompetitorName = DataBinder.GetPropertyValue(data, this.DataCompetitorNameField, null);

The nice thing is that you never have to know the actual type that the control is databound to. And with that, we now have the ability to bind a bracket directly to a database table of teams, and the whole bracket structure can be completely dynamic, and the developer using the Tourney Bracket Control never has to know what the final bracket will look like.

All in all, setting up databinding turned out to be pretty simple. I'm glad we went through the process because next time, it won't be nearly as daunting as it was this time!

Categories: ASP.NET