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

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

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

ObjectDataSource 預設提供了三個屬性,讓我們可以進行分頁及排序,這三個屬性會對應到 ObjectDataSource.SelectMethod 的方法參數

MaximumRowsParameterName

StartRowIndexParameterName

SortParameterName

 

但如果需要擴充查詢條件呢?要怎麼做?

image

 

在 Employee.cs,新增 Location 屬性

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

 

本篇章節

@Location_ObjectDataSource

前端:
後端:

@Location_DropDownList

前端:
後端:

@Employee_ObjectDataSource

前端:
後端:

@Employee_GridView

前端:


@Location_ObjectDataSource

我希望它能把所有的 Location 欄位的資料取出來並濾掉重複的

後端:

public IEnumerable<string> GetLocations()
{
    return this.m_Employees.Select(p => p.Location).Distinct();
}

 

前端:

    OldValuesParameterFormatString="original_{0}"
    SelectMethod="GetLocations"
    TypeName="Simple.ODS.MultiParameterPaging.EmployeeDataAccess"></asp:ObjectDataSource>

 

@Location_DropDownList

前端:

綁定成功後,我要在第一個位插入"ALL"

    DataSourceID="Location_ObjectDataSource"
    OnDataBound="Location_DropDownList_DataBound">
</asp:DropDownList>

 

後端:

判斷有無 ALL 項目

{
    var ddl = (DropDownList)sender;
    var isExists = false;
    foreach (ListItem item in ddl.Items)
    {
        if (item.Value == "ALL")
        {
            isExists = true;
            break;
        }
    }
    if (!isExists)
    {
        ddl.Items.Insert(0, "ALL");
        ddl.Items[0].Value = "ALL";
        ddl.SelectedIndex = 0;
    }
}

 

@Employee_ObjectDataSource

後端:

準備後端的查詢邏輯方法,這裡我多了一個 location 查詢參數,並增加 ALL 查詢判斷,ALL 表示不過濾

PS.orderBy會依不同的控制項而傳入不同的結果,請依照您實際使用的控制項自行處理

public IEnumerable<Employee> GetEmployees(string location, int maximumRows, int startRowIndex, string orderBy)
{
    Func<Employee, bool> condition = null;
    if (location == "ALL")
    {
        condition = e => true;
    }
    else
    {
        condition = e => e.Location == location;
    }
    if (string.IsNullOrWhiteSpace(orderBy))
    {
        return this.m_Employees.Where(condition)
                               .OrderBy("Id")
                               .Skip(startRowIndex)
                               .Take(maximumRows);
    }

    //控制項的行為都不一樣,所得到的orderBy也會不一樣
    if (orderBy.Contains("DESC"))
    {
        var split = orderBy.Split(' ');
        var columnName = split[0];
        return this.m_Employees.Where(condition)
                               .OrderByDescending(columnName)
                               .Skip(startRowIndex)
                               .Take(maximumRows);
    }
    else
    {
        return this.m_Employees.Where(condition)
                               .OrderBy(orderBy)
                               .Skip(startRowIndex)
                               .Take(maximumRows);
    }
}

 

再來,GetEmployeeCount 的參數也要跟 GetEmployees 一樣,也要有 ALL 判斷邏輯

public int GetEmployeeCount(string location, int maximumRows, int startRowIndex, string orderBy)
{
    Func<Employee, bool> condition = null;
    if (location == "ALL")
    {
        condition = e => true;
    }
    else
    {
        condition = e => e.Location == location;
    }
    var queryCount = this.m_Employees.Count(condition);
    return queryCount;
}

 

前端:

原本的

MaximumRowsParameterName(刪除)

StartRowIndexParameterName(刪除)

SortParameterName(保留)

新增四個 SelectParameters 參數

 

 

    OldValuesParameterFormatString="original_{0}"
    DataObjectTypeName="Simple.ODS.MultiParameterPaging.Employee"
    TypeName="Simple.ODS.MultiParameterPaging.EmployeeDataAccess"
    InsertMethod="Insert"
    DeleteMethod="Delete"
    UpdateMethod="Update"
    SelectCountMethod="GetEmployeeCount"
    SelectMethod="GetEmployees"
    SortParameterName="orderBy"
     EnablePaging="True">
    <SelectParameters>
        <asp:ControlParameter ControlID="Location_DropDownList" Name="location" Type="String" />
        <asp:Parameter Name="maximumRows" Type="Int32" />
        <asp:Parameter Name="startRowIndex" Type="Int32" />
        <asp:Parameter Name="orderBy" Type="String" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="Birthday" Type="DateTime" />
    </UpdateParameters>
    <InsertParameters>
        <asp:Parameter Name="Birthday" Type="DateTime" />
    </InsertParameters>
</asp:ObjectDataSource>

 

@Employee_GridView

前端:

最後綁定 Employee_ObjectDataSource 即可

    ForeColor="#333333" CellPadding="4"
    AllowPaging="True"
    PageSize="3"
    AllowSorting="True"
    DataKeyNames="Id">

    <Columns>
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
        <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
        <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
        <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
        <asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
        <asp:BoundField DataField="Birthday" HeaderText="Birthday" SortExpression="Birthday" />
    </Columns>
</asp:GridView>

 


前端程式碼:https://dotblogsamples.codeplex.com/SourceControl/latest#Simple.ODS.MultiParameterPaging/Simple.ODS.MultiParameterPaging/Views/Default.aspx

 

後端程式碼:https://dotblogsamples.codeplex.com/SourceControl/latest#Simple.ODS.MultiParameterPaging/Simple.ODS.MultiParameterPaging/Views/Default.aspx.cs

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

文章出自:http://www.dotblogs.com.tw/yc421206/archive/2014/11/28/147462.aspx

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


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

Image result for microsoft+mvp+logo