[C#.NET][ASP.NET] Use ObjectDataSource + LINQ Achieve Paging and Sorting in WebForm Part1

[C#.NET][ASP.NET] Use ObjectDataSource + LINQ Achieve Paging and Sorting in WebForm Part1

續上篇,http://www.dotblogs.com.tw/yc421206/archive/2014/11/13/147293.aspx

這次要實作分頁及排序,本文章節如下:

 


SelectCountMethod & SelectMethod at ObjectDataSource

SelectCountMethod:

後端,建立 GetEmployeeCount(),用來取得資料表總數量


public int GetEmployeeCount() 
{ 
    return this.m_Employees.Count; 
}

this.m_Employees 是我手動建立的集合物件,參考以下

https://dotblogsamples.codeplex.com/SourceControl/latest#Simple.ObjectDataSourcePaging/Simple.ObjectDataSourcePaging/DataAccess/EmployeeDataAccess.cs

 

前端,在 ObjectDataSource 定義 SelectCountMethod="GetEmployeeCount"


    OldValuesParameterFormatString="original_{0}"
    DataObjectTypeName="Simple.ObjectDataSourcePaging.Models.Employee"
    TypeName="Simple.ObjectDataSourcePaging.DataAccess.EmployeeDataAccess"
    SelectCountMethod="GetEmployeeCount">
</asp:ObjectDataSource>

 

SelectMethod:

後端,建立 GetEmployees(),用來取得分頁資訊,該方法的參數會對應到前端的設定


public IEnumerable<Employee> GetEmployees(int maximumRows, int startRowIndex, string orderBy) 
{ 
    if (string.IsNullOrWhiteSpace(orderBy))//網頁第一次進來orderby為空 
    { 
        orderBy = "Id"; 
    } 
    return this.m_Employees.OrderBy(orderBy).Skip(startRowIndex).Take(maximumRows); 
}

PS.this.m_Employees.OrderBy(orderBy)是上篇寫的擴充方法

http://www.dotblogs.com.tw/yc421206/archive/2014/11/25/147419.aspx

前端,SelectMethod="GetEmployees",GetEmployees 指的是後端方法

參數名稱要與方法相同

MaximumRowsParameterName="maximumRows"
StartRowIndexParameterName="startRowIndex"
SortParameterName="orderBy"

再來開啟 EnablePaging="True"

完成結果如下:

 
    OldValuesParameterFormatString="original_{0}" 
    DataObjectTypeName="Simple.ObjectDataSourcePaging.Models.Employee" 
    TypeName="Simple.ObjectDataSourcePaging.DataAccess.EmployeeDataAccess" 
    SelectCountMethod="GetEmployeeCount" 
    SelectMethod="GetEmployees" 
    MaximumRowsParameterName="maximumRows" 
    StartRowIndexParameterName="startRowIndex" 
    SortParameterName="orderBy" 
    EnablePaging="True">

</asp:ObjectDataSource>

 

AllowSorting &  AllowPaging at GridView

最後把 GridView 的下面三個屬性打開

AllowPaging="True" 
PageSize="3" 
AllowSorting="True"

完成結果如下:


    DataKeyNames="Id" 
    AllowPaging="True" 
    PageSize="3" 
    AllowSorting="True"> 
    <Columns> 
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" /> 
        <asp:BoundField DataField="Id" HeaderText="流水號" SortExpression="Id" /> 
        <asp:BoundField DataField="Name" HeaderText="姓名" SortExpression="Name" /> 
        <asp:BoundField DataField="Age" HeaderText="年齡" SortExpression="Age" /> 
        <asp:BoundField DataField="Email" HeaderText="信箱" SortExpression="Email" /> 
        <asp:BoundField DataField="Birthday" HeaderText="生日" SortExpression="Birthday" /> 
    </Columns> 
</asp:GridView>

 

最後,因為欄位有日期別忘了把 UpdateParameters & InsertParameters 加上去


    OldValuesParameterFormatString="original_{0}"
    DataObjectTypeName="Simple.ObjectDataSourcePaging.Models.Employee"
    TypeName="Simple.ObjectDataSourcePaging.DataAccess.EmployeeDataAccess"
    InsertMethod="Insert"
    DeleteMethod="Delete"
    UpdateMethod="Update"
    SelectCountMethod="GetEmployeeCount"
    SelectMethod="GetEmployees"
    MaximumRowsParameterName="maximumRows"
    StartRowIndexParameterName="startRowIndex"
    SortParameterName="orderBy"
    EnablePaging="True">
    <UpdateParameters>
        <asp:Parameter Name="Birthday" Type="DateTime" />
    </UpdateParameters>
    <InsertParameters>
        <asp:Parameter Name="Birthday" Type="DateTime" />
    </InsertParameters>
</asp:ObjectDataSource>

 

 

前端完整程式碼:

https://dotblogsamples.codeplex.com/SourceControl/latest#Simple.ObjectDataSourcePaging/Simple.ObjectDataSourcePaging/Views/Default.aspx

 

後端完整程式碼:

https://dotblogsamples.codeplex.com/SourceControl/latest#Simple.ObjectDataSourcePaging/Simple.ObjectDataSourcePaging/Models/Employee.cs

https://dotblogsamples.codeplex.com/SourceControl/latest#Simple.ObjectDataSourcePaging/Simple.ObjectDataSourcePaging/DataAccess/EmployeeDataAccess.cs

https://dotblogsamples.codeplex.com/SourceControl/latest#Simple.ObjectDataSourcePaging/Simple.ObjectDataSourcePaging/Extensions/LinqExtension.cs

 


本文出自:http://www.dotblogs.com.tw/yc421206/archive/2014/11/25/147421.aspx

專案位置:https://dotblogsamples.codeplex.com/SourceControl/latest#Simple.ObjectDataSourcePaging/

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo