由 Tom FitzMacken 撰寫
本教學系列示範在 ASP.NET Web Forms 專案中使用模型綁定的基本面向。 模型綁定使資料互動比處理資料來源物件(如 ObjectDataSource 或 SqlDataSource)更為直接。 這個系列從入門教材開始,後續教學會進入更進階的概念。
本教學展示了如何透過模型綁定加入排序、分頁與資料過濾功能。
本教學建立在系列第一 部分 所建立的專案基礎上。
你可以 下載 使用 C# 或 VB 的完整專案。 可下載的程式碼可搭配 Visual Studio 2012 或 Visual Studio 2013 使用。 它使用 Visual Studio 2012 模板,與本教學中展示的 Visual Studio 2013 模板略有不同。
你將會建構什麼
在本教學課程中,您將會:
- 啟用資料的排序與分頁功能
- 啟用使用者根據選擇篩選資料
新增排序功能
在 GridView 中啟用排序非常簡單。 在 Student.aspx 檔案中,只要在 GridView 中將 AllowSorting 設 為 true 。 你不需要為每欄設定 SortExpression 值,因為 DataField 會自動使用。 GridView 會修改查詢,加入依選定值排序資料。 下方標示的程式碼顯示你需要新增的部分,才能啟用排序功能。
<asp:GridView runat="server" ID="studentsGrid"
ItemType="ContosoUniversity.Models.Student" DataKeyNames="StudentID"
SelectMethod="studentsGrid_GetData"
UpdateMethod="studentsGrid_UpdateItem" DeleteMethod="studentsGrid_DeleteItem"
AllowSorting="true"
AutoGenerateEditButton="true" AutoGenerateDeleteButton="true"
AutoGenerateColumns="false">
<Columns>
<asp:DynamicField DataField="StudentID" />
<asp:DynamicField DataField="LastName" />
<asp:DynamicField DataField="FirstName" />
<asp:DynamicField DataField="Year" />
<asp:TemplateField HeaderText="Total Credits">
<ItemTemplate>
<asp:Label Text="<%# Item.Enrollments.Sum(en => en.Course.Credits) %>"
runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
執行網頁應用程式,並依不同欄位的數值測試學生紀錄排序。
新增分頁功能
啟用分頁也非常簡單。 在 GridView 中,將 AllowPaging 屬性設 為 true ,並將 PageSize 屬性設定為你希望在每個頁面顯示的紀錄數量。 在這個教學中,你可以設定為4。
<asp:GridView runat="server" ID="studentsGrid"
ItemType="ContosoUniversity.Models.Student" DataKeyNames="StudentID"
SelectMethod="studentsGrid_GetData"
UpdateMethod="studentsGrid_UpdateItem" DeleteMethod="studentsGrid_DeleteItem"
AllowSorting="true" AllowPaging="true" PageSize="4"
AutoGenerateEditButton="true" AutoGenerateDeleteButton="true"
AutoGenerateColumns="false">
執行網頁應用程式,發現現在紀錄被分成多個頁面,單一頁面最多顯示四筆紀錄。
延遲查詢執行提升了應用程式的效率。 GridView 不會取得整個資料集,而是修改查詢,只取得當前頁面的紀錄。
依使用者選擇篩選紀錄
模型綁定新增了幾個屬性,讓你能指定如何在模型綁定方法中設定參數的值。 這些屬性位於 System.Web.ModelBinding 命名空間中。 其中包括:
- 管理
- 餅乾
- 表單
- 個人檔案
- 查詢字串
- 路線資料
- Session
- 用戶檔案
- 檢視狀態 (ViewState)
在這個教學中,你會使用控制項的值來篩選 GridView 顯示的紀錄。 你可以把 Control 屬性加入你先前建立的查詢方法。 在 後續 的教學中,你會將 QueryString 屬性套用到參數上,指定該參數值來自查詢字串的值。
首先,在 ValidationSummary 上方,新增一個下拉選單,用來篩選顯示哪些學生。
<asp:HyperLink runat="server" NavigateUrl="~/AddStudent" Text="Add New Student" />
<br /><br />
<asp:Label runat="server" Text="Show:" />
<asp:DropDownList runat="server" AutoPostBack="true" ID="DisplayYear">
<asp:ListItem Text="All" Value="" />
<asp:ListItem Text="Freshman" />
<asp:ListItem Text="Sophomore" />
<asp:ListItem Text="Junior" />
<asp:ListItem Text="Senior" />
</asp:DropDownList>
<asp:ValidationSummary runat="server" ShowModelStateErrors="true"/>
在程式碼後方檔案中,修改 select 方法以接收控制項的值,並將參數名稱設為提供該值的控制項名稱。
你必須為 System.Web.ModelBinding 命名空間新增 using 語句,才能解析 Control 屬性。
using System.Web.ModelBinding;
以下程式碼顯示了重新設計的 select 方法,根據下拉選單的值來過濾回傳的資料。 在參數前加上控制屬性,表示該參數的值來自同名控制項。
public IQueryable<Student> studentsGrid_GetData([Control] AcademicYear? displayYear)
{
SchoolContext db = new SchoolContext();
var query = db.Students.Include(s => s.Enrollments.Select(e => e.Course));
if (displayYear != null)
{
query = query.Where(s => s.Year == displayYear);
}
return query;
}
執行網頁應用程式,從下拉選單中選擇不同值來篩選學生名單。
結論
在這個教學中,你啟用了資料的排序和分頁功能。 你也啟用了以控制值來篩選資料的功能。
在下一個 教學 中,你將透過將 JQuery UI 小工具整合進動態資料範本,來強化使用者介面。