Lessons in this course (10)

6. Data binding — GridView, Repeater, ListView and data source controls

Intermediate★ Must knowLesson 6 of 10
ControlOutputPaging/sorting/editing built inUse
GridViewHTML table✅Admin grids with edit/delete
RepeaterExactly your template HTML❌ (do it yourself)Lightweight custom lists
ListViewYour templates + layout✅ (with DataPager)Flexible lists with editing
DataListTable/flow layoutPartialLegacy
DetailsView / FormViewOne 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.