[Excalibur]ASP.NET MVVM - ExtensionsControl (1)

控制項介紹FreeDataSource,Pagination

這兩天在ASP.NET MVVM Excalibur中加入了幾個使用者控制項
我一直在想有無好一點的方法去處理ViewModel對陣列集合資料的繫結功能
結果還是先把平日自己開發來經常使用在集合資料的控制項加進去了
以下是對這幾個控制項的介紹與示例程式

 

首先是FreeDataSource跟Pagination的功能

 

1.FreeDataSource是一個資料來源控制項如同SqlDataSource,ObjectDataSource,EntityDataSource等一樣 用來搭配ListView,GridView,DataList..等等控制項呈現資料
其特性是沒有其他限制,讓我們可以直接使用事件的回傳值當作資料來源
無論在方法中回傳什麼也都盡可能讓它能支援分頁排序等功能
例如下面的方法:


protected IEnumerable FreeDataSource1_ExecuteSelected(object sender)
{
    List<object> list = new List<object>();
    for (int i = 1; i <= 100; i++)
    {
        list.Add(new
        {
            Text = i.ToString()
        });
    }
    return list;
}

就能直接讓回傳的IEnumerable物件資料用做DataBind的資料這樣

 

2.Pagination是分頁用的控制項搭配FreeDataSource與DataPager製作分頁功能
使用時需要在Pagination上指定PagerControlID以及在FreeDataSource上將PaginationControlID
設定為該Pagination的ID


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ExtensionsControl(1) : FreeDataSource, Pagination</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <uc1:FreeDataSource ID="FreeDataSource1" runat="server" PaginationControlID="Pagination1"
                OnExecuteSelected="FreeDataSource1_ExecuteSelected" />
            <asp:ListView ID="ListView1" runat="server" DataSourceID="FreeDataSource1">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Text") %>'></asp:Label>
                </ItemTemplate>
            </asp:ListView>
            <br />
            <uc2:Pagination ID="Pagination1" runat="server" PagerControlID="DataPager1"
                OnSelectedPageChanging="Pagination1_SelectedPageChanging" />
            <asp:DataPager ID="DataPager1" runat="server" PageSize="5">
                <Fields>
                    <asp:NextPreviousPagerField ShowNextPageButton="false" />
                    <asp:NumericPagerField ButtonCount="5" />
                    <asp:NextPreviousPagerField ShowPreviousPageButton="false" />
                </Fields>
            </asp:DataPager>
        </div>
    </form>
</body>
</html>

執行結果:

順利完成分頁
(其實還可以在FreeDataSource的方法中透過取得的頁數進行分頁處理)

 

這裡主要是想當我們使用MVVM時可以讓ViewModel使用Binding去繫結FreeDataSource的
OnExecuteSelected事件,然後就能將資料DataBind到要顯示資料的ListView或GridView上面,
以達到控制Model資料的權責歸於ViewModel,與View的呈現邏輯分離

 

另外還有加入PluralHolder, ContainerButton, HolderSource三個控制項
是有關於控制動態樣板的功能,在編輯多筆資料時也許有用
與示範程式一併都放在1.1.1.3版以後的專案範本中