Roadmap › ASP.NET Web Forms — Page Life Cycle, ViewState, Controls and State Management › Module: Building Pages
Lessons in this course (10)
6. Data binding — GridView, Repeater, ListView and data source controls
Intermediate★ Must knowLesson 6 of 10
| Control | Output | Paging/sorting/editing built in | Use |
|---|---|---|---|
| GridView | HTML table | ✅ | Admin grids with edit/delete |
| Repeater | Exactly your template HTML | ❌ (do it yourself) | Lightweight custom lists |
| ListView | Your templates + layout | ✅ (with DataPager) | Flexible lists with editing |
| DataList | Table/flow layout | Partial | Legacy |
| DetailsView / FormView | One record | ✅ | Detail/edit forms |
aspx
<asp:Repeater ID="rptOrders" runat="server" ItemType="ShopWeb.Models.OrderDto">
<ItemTemplate>
<div class="order"><%#: Item.Id %> — <%#: Item.Total.ToString("C") %></div>
</ItemTemplate>
</asp:Repeater>
<!-- ASP.NET 4.5 model binding: the grid calls your methods -->
<asp:GridView ID="gv" runat="server" ItemType="ShopWeb.Models.Product" DataKeyNames="Id"
SelectMethod="GetProducts" UpdateMethod="UpdateProduct" AllowPaging="true" AllowSorting="true" AutoGenerateEditButton="true" />csharp
public IQueryable<Product> GetProducts() => _db.Products.OrderBy(p => p.Name); // paging/sorting applied by the grid
public void UpdateProduct(int id)
{
var p = _db.Products.Find(id);
if (p != null && TryUpdateModel(p)) _db.SaveChanges();
}Data source controls: SqlDataSource (SQL in markup — quick but mixes data access into the UI), ObjectDataSource (calls methods on your business class — better layering), EntityDataSource, LinqDataSource. Prefer model binding or code-behind binding to services.
GridView events: RowDataBound (format each row), RowCommand (custom buttons with CommandName/CommandArgument), RowEditing/RowUpdating/RowDeleting, PageIndexChanging, Sorting.