| 控件 | 繼承鏈 | 渲染結構 | 核心優勢 | 典型適用場景 |
|---|---|---|---|---|
| GridView | WebControl → BaseDataBoundControl | 強制 <table><tr><td> | 內建分頁/排序/編輯/刪除,零代碼 CRUD | 後台管理、簡單報表、快速原型 |
| DataList | WebControl → BaseDataList | <table> 但可通過 RepeatColumns/RepeatDirection 佈局 | 樣式屬性豐富,支援流式/重複佈局 | 產品目錄、圖片牆、多列佈局 |
| Repeater | Control → BaseDataList | 無預設標籤,完全由模板決定 | HTML 完全可控,效能最高,無 ViewState 擴展 | 前台頁面、SEO 敏感、高性能列表 |
WebControl,擁有 CssClass、ForeColor、BorderStyle 等樣式屬性,最終輸出帶大量內聯樣式的 <table>。Control,零標記輸出,開發者在 ItemTemplate 中寫什麼就輸出什麼(<div>、<ul>、<article> 任意)。| 功能 | GridView | DataList | Repeater |
|---|---|---|---|
| 分頁 | ✅ AllowPaging | ❌ 自行實現 | ❌ 自行實現 |
| 排序 | ✅ AllowSorting | ❌ | ❌ |
| 行編輯/刪除 | ✅ 命令事件 | ✅ EditCommand 等 | ❌ 純模板 |
| 選中行 | ✅ SelectedIndex | ✅ SelectedIndex | ❌ |
| 模板類型 | 7 種 | 7 種 | 5 種(無 Edit/Selected) |
需要零代碼增刪改查 + 後台管理? → GridView
需要多列流式佈局 + 豐富樣式屬性? → DataList
前台展示 + SEO + 高性能 + 完全控制 HTML? → Repeater
大數據量 + 自定義分頁/排序邏輯? → Repeater + 手工分頁
AllowPaging 會 全表查詢後內存分頁。百萬級數據必須改用 ObjectDataSource + 存儲過程分頁 或 手工綁定 PageIndexChanging 事件執行 ROW_NUMBER() 分頁查詢。ItemDataBound 中 FindControl 綁定,或模板中用 DataSource='<%# Eval("ChildCollection") %>'。CssClass,配合 CSS 類管理外觀,避免 !important 大戰。<article>/<section>)天然優於 <table> 佈局。ASP.NET Core / Razor Pages / Blazor 時代:
@foreach (var item in Model) { <partial name="_Item" model="item" /> } 替代 Repeater。GridComponent、ListComponent,內部封裝分頁、排序邏輯。理解三大控件的本質差異,能幫助你在維護舊專案時做最優選擇,在新專案中避免過度工程化,選擇最適合的技術棧。