Theory
One list, many rows, no copy-paste
FestConnect has a list of events, maybe 30 of them, and each needs a row on screen: name, date, venue. You are not going to hand-write 30 blocks of HTML, and you certainly will not re-edit them every time the event list changes.
Instead you hand the whole list to a control and let it draw every row for you. That is data binding: connecting a source of data to a control that displays it. Change the underlying list and the display follows. This lesson covers data binding and the main controls that use it: GridView, ListBox, Repeater and FormView.
Theory
How data binding works
The pattern is always the same two steps. You set the control's DataSource to your data (a list, a DataTable, the results of a query), then you call DataBind(). The control walks through the data and renders one entry per item.
That is the whole idea: give it the data, tell it to bind, and it builds the display. If the data has 5 events, you get 5 rows; if it has 50, you get 50, with no change to your code. The control handles the repetition so you do not have to.
Practical
Binding the FestConnect event list to a GridView
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// 'events' could come from a database; a list is shown here for clarity
var events = GetEvents(); // e.g. a list of Event objects
gvEvents.DataSource = events; // 1) point the control at the data
gvEvents.DataBind(); // 2) render one row per item
}
}
// gvEvents is an <asp:GridView> in the markup. It now shows every event.At a glance
| Control | Shows | Layout control |
|---|---|---|
| GridView | A grid: rows and columns, like a table | Built in; supports sorting and paging |
| ListBox | A scrollable list of selectable items | Simple list; user can pick |
| Repeater | Whatever HTML template you write, repeated | Total: you supply the template, no default |
| FormView | One record at a time, your template | One item, fully custom layout |
Formula
DataBind() and per-item events
Nothing appears until you call DataBind(); setting DataSource alone does not render anything. Remember to bind (typically inside if (!IsPostBack), so the grid is not needlessly rebuilt on every click).
As a control binds, it can raise data-binding events, one per item. A GridView's RowDataBound fires for each row as it is built, letting you customise it: for example, colour a 'Full' event red, or format a date. These events are your hook to adjust individual items during binding.
Quiz
You want to show FestConnect's events as a table with columns, sorting, and paging, with minimal effort. Which control fits best?
- Repeater, because it has the most built-in table features
- GridView, which renders rows and columns as a grid and supports sorting and paging out of the box
- FormView, because it shows all records as a grid
- ListBox, because it displays columns automatically
Show the answer
GridView, which renders rows and columns as a grid and supports sorting and paging out of the box
GridView is the tabular grid control: it renders rows and columns and has built-in sorting and paging, so it fits a table of events with least effort. Option A is backwards: the Repeater has NO built-in layout at all; you must write the entire template yourself, which is the opposite of minimal effort for a standard table. Option C is wrong because FormView shows ONE record at a time, not a grid of all of them. Option D is wrong: a ListBox is a single-column scrollable list of selectable items, not a multi-column grid. Match the control to the shape of the display: grid of many rows -> GridView.
Think first
So when would you pick a Repeater over a GridView?
If GridView does so much automatically, why does the Repeater, which gives you nothing by default, even exist? Then tap.
Show the answer
You choose a Repeater when you want COMPLETE control over the HTML and do not want the grid's built-in table structure. A GridView is quick but it renders a table with its own markup, which is great for a spreadsheet-like grid but limiting if you want, say, each event shown as a styled card, or a custom list of divs, or markup that fits a specific design. The Repeater renders exactly the template you write, once per item, and nothing else: no automatic table, no extra tags. That freedom is the point. The trade-off is effort and features: with a Repeater you write all the layout yourself and you do not get free sorting or paging. So the rule is: GridView for a standard data grid with least work; Repeater when the design demands custom markup and you are happy to write it. FormView is the same idea for a single record. Right tool, right shape.
Summary
Key takeaways
- Data binding connects a data source to a control that displays every item automatically.
- The pattern is two steps: set the control's DataSource, then call DataBind(); nothing renders until you bind.
- GridView shows a grid of rows and columns with built-in sorting and paging.
- ListBox is a scrollable list of selectable items; Repeater renders a fully custom template you write, per item; FormView shows one record at a time with a custom template.
- Data-binding events like GridView's RowDataBound fire per item, letting you customise each row as it is built.
- Bind inside if (!IsPostBack) so the control is not needlessly rebuilt on every postback.
- Memory hook: DataSource + DataBind() = the control draws every row for you.