Populate an ASP.NET view with data
I've found four ways to populate an ASP.NET view with data.
- Databind from Page_Load in code behind
- OnLoad event on the databinding control
- Fetching data from a method with databound expression
- DataSourceControl
Databind from Page_Load in code behind
When I see this happening I automatically removes it and blame the developer who ever put it there. My Page_Load should be as clean as possible. If there is a way to remove code from the Page_Load I will attempt to do it.
This is a big no no!
OnLoad event on the databinding control
There is a method where you use the OnLoad-event to databind the control. The good thing about this is a clear separation of concern between the control being databound and the data that is bound.
protected void Names_DataBind(object sender, EventArgs e) { var control = (BaseDataBoundControl)sender; control.DataSource = new[] { "James", "Joe", "Jimmy" }; control.DataBind(); }
I find this much neater. The method responsible for finding the data has no idea on what control is used for databinding.
Fetching data from a method with databound expression
Here comes something similar to the previous example. We act at the event OnLoad and databind the control itself with a databoundexpression.
protected void DataBind(object sender, EventArgs e) { ((Control)sender).DataBind(); }
protected string[] GetFlowers() { return new[] { "Rose", "Lily", "Dragonflower" }; }
In this example I would place the DataBind callback method in a common PageBase or UserControlBase where it could be reused all over the application. The power of this method of databinding is reusing dataproviding method with only changing arguments to the method from markup. This is something that is not possible with our previous method of databinding. You should beware of putting logic in the databound expression since it is inline code in markup.
DataSourceControl
The most powerful method of databinding is implementing a DataSourceControl. This is useful when you have a common data store that you want to get data from, and use different views to display parts of that data. This is the most concern separated application here, but also the most time consuming and complex. Use this when you notice that you query the same data in more than three different ways or from three different places in your code.