English Version: http://dflying.dflying.net/1/archive/127_paging_your_list_using_aspnet_atlas_pagenavigator_control.html
在這個(gè)系列中,我將介紹一些Atlas Sys.UI.Data中較高級的控件,包括:
Sys.UI.Data.ListView:使用asp.net Atlas ListView控件顯示列表數(shù)據(jù)
Sys.UI.Data.ItemView:使用ASP.NET Atlas ItemView控件顯示集合中的單個(gè)數(shù)據(jù)
Sys.UI.Data.DataNavigator:使用 ASP.NET Atlas PageNavigator控件實(shí)現(xiàn)客戶端分頁導(dǎo)航
Sys.UI.Data.SortBehavior:待續(xù)
Sys.UI.Data.XSLTView:待續(xù)
這篇是其中的第三篇:使用 ASP.NET Atlas PageNavigator控件實(shí)現(xiàn)客戶端分頁導(dǎo)航
把所有的記錄統(tǒng)統(tǒng)放在一個(gè)頁面上絕對不是一個(gè)好主意,特別是當(dāng)您有成百上千條記錄時(shí)。您的用戶需要不停的拖動(dòng)滾動(dòng)條,甚至使用Control+F來找到所期待的內(nèi)容,這將帶來相當(dāng)差的用戶體驗(yàn)。這時(shí),將數(shù)據(jù)以分頁的方式顯示給用戶將友好的多。一些ASP.NET服務(wù)器端控件擁有內(nèi)建的分頁及頁面導(dǎo)航功能,例如DataGrid和GridView。同樣的,Atlas客戶端控件Sys.UI.Data.DataNavigator也提供了類似的功能,這將大大提高我們的開發(fā)效率。
DataNavigator控件將與DataView(請參考:Atlas命名空間Sys.Data下控件介紹——DataView和DataFilter )控件一起工作。我們知道DataView控件沒有提供頁面導(dǎo)航相關(guān)方法,所以我們只能直接設(shè)置它的pageIndex屬性來實(shí)現(xiàn)導(dǎo)航。雖然沒有什么難度,但很多情況下這并不是一個(gè)好辦法,因?yàn)橄裎疫@樣好多粗心的開發(fā)者往往會忘記檢查pageIndex的邊界值,造成不必要的麻煩。這也是Atlas要提供DataNavigator控件的原因之一,DataNavigator控件將作為一個(gè)DataView控件的代理(PRoxy),提供易用的頁面導(dǎo)航接口。
DataNavigator對象只有一個(gè)屬性:
dataView:對某個(gè)DataView對象的引用,這個(gè)DataNavigator將把頁面導(dǎo)航的操作應(yīng)用到其上。您應(yīng)該總是指定這個(gè)屬性。
另外,要使用DataNavigator控件,您還需要提供一些擁有一些指定commandName屬性的Atlas Button,以觸發(fā)相應(yīng)的頁面導(dǎo)航操作。這些Button的parent屬性應(yīng)該設(shè)定為此DataNavigator控件,以保證DataNavigator能夠捕獲到這些Button發(fā)出的命令。
您可以指定您的Button的commandName屬性為如下五個(gè)string,每個(gè)都有不同的含義:
page:將當(dāng)前頁面索引轉(zhuǎn)為命令參數(shù)(command argument)中指定的值。通過這個(gè)命令我們可以快速的改變頁面的索引。
nextpage:切換到下一頁(如果存在下一頁)。
previouspage:切換到上一頁(如果存在上一頁)。
firstpage:切換到第一頁。
lastpage:切換到最后一頁。
OK,MSDN般枯燥的介紹到此為止吧,讓我們通過一個(gè)實(shí)例來熟悉DataNavigator的使用方法。
首先我們需要暴露一個(gè)Web Service,以便Atlas頁面使用。該Web Service將返回100條記錄。下面就是這個(gè)Web Service的代碼,非常易于理解,這里不贅。
Web Service
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Web;
using System.Web.Caching;
using System.Web.Services;
using System.Web.Services.Protocols;
using Microsoft.Web.Services;
//
// For simplicity this example demonstraes storing and manipulating
// the data objects in memory. A database can also be used.
//
[WebService(Namespace = "[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MyDataService : DataService
{
static List<Entry> _data;
static object _dataLock = new object();
private static List<Entry> Data
{
get
{
if (_data == null)
{
lock (_dataLock)
{
if (_data == null)
{
_data = new List<Entry>();
for (int i = 0; i < 100; i++)
{
_data.Add(new Entry(i, "Dflying " + i.ToString(), string.Format("Dflying{0}@dflying.net", i.ToString())));
}
}
}
}
return _data;
}
}
[DataObjectMethod(DataObjectMethodType.Select)]
public Entry[] SelectRows()
{
return MyDataService.Data.ToArray();
}
}
public class Entry
{
private string _name;
private string _email;
private int _id;
[DataObjectField(true, true)]
public int Id
{
get { return _id; }
set { _id = value; }
}
[DataObjectField(false)]
[DefaultValue("New row")]
public string Name
{
get { return _name; }
set { _name = value; }
}
[DataObjectField(false)]
[DefaultValue("")]
public string Email
{
get { return _email; }
set { _email = value; }
}
public Entry()
{
_id = -1;
}
public Entry(int id, string name, string description)
{
_id = id;
_name = name;
_email = description;
}
}
然后,在ASPX頁面中我們需要考慮并定義如下四部分的內(nèi)容:
一個(gè)ScriptManager控件,用來包含頁面必須的Atlas Framework相關(guān)腳本文件。通常情況下,這也是每個(gè)Atlas頁面必須包含的。
一個(gè)占位(place holder)的div(id為dataContents,見代碼)。Atlas將會把渲染后的分頁的ListView放置于此。
一個(gè)作為容器的div(DataNavigator控件),以及其中包含的一組按鈕(命令按鈕),用來實(shí)現(xiàn)頁面導(dǎo)航功能。
一個(gè)隱藏的div,用來放置ListView的模版。
下面是以上四部分內(nèi)容的代碼,關(guān)于ListView控件的模版,請參考我的這篇文章:使用ASP.NET Atlas ListView控件顯示列表數(shù)據(jù)
<!-- ScriptManager -->
<atlas:ScriptManager runat="server" ID="scriptManager" />
<!-- Element for paged ListView (container) -->
<div id="dataContents">
</div>
<!-- PageNavigator -->
<div id="pageNavigator">
<input type="button" id="btnFirstPage" value="<<" />
<input type="button" id="btnPrevPage" value="<" />
<span id="lblPageNumber"></span> / <span id="lblPageCount"></span>
<input type="button" id="btnNextPage" value=">" />
<input type="button" id="btnLastPage" value=">>" />
</div>
<!-- Templates -->
<div style="visibility: hidden; display: none">
<table id="myList_layoutTemplate" border="1" cellpadding="3" style="width:20em;">
<thead>
<tr>
<td><span>No.</span></td>
<td><span>Name</span></td>
<td><span>Email</span></td>
</tr>
</thead>
<!-- Repeat Template -->
<tbody id="myList_itemTemplateParent">
<!-- Repeat Item Template -->
<tr id="myList_itemTemplate">
<td><span id="lblIndex" /></td>
<td><span id="lblName" /></td>
<td><span id="lblEmail" /></td>
</tr>
</tbody>
</table>
<!-- Empty Template -->
<div id="myList_emptyTemplate">
No Data
</div>
</div>
最后該書寫Atlas的xml腳本定義了,有如下五個(gè)部分:
第一部分:Atlas客戶端控件DataSource,用來從我們上面定義的Web Service中取得數(shù)據(jù)。
<dataSource id="dataSource" autoLoad="true" serviceURL="MyDataService.asmx" />
第二部分:一個(gè)DataView控件(請參考:Atlas命名空間Sys.Data下控件介紹——DataView和DataFilter ),用來將第一部分中取得的那100條數(shù)據(jù)分頁。
<dataView id="view" pageSize="12">
<bindings>
<binding dataContext="dataSource" dataPath="data" property="data" />
</bindings>
</dataView>
第三部分:一個(gè)ListView控件(請參考: 使用ASP.NET Atlas ListView控件顯示列表數(shù)據(jù) ),用于顯示分頁好的數(shù)據(jù)。
<listView id="dataContents" itemTemplateParentElementId="myList_itemTemplateParent" >
<bindings>
<binding dataContext="view" dataPath="filteredData" property="data"/>
</bindings>
<layoutTemplate>
<template layoutElement="myList_layoutTemplate"/>
</layoutTemplate>
<itemTemplate>
<template layoutElement="myList_itemTemplate">
<label id="lblIndex">
<bindings>
<binding dataPath="$index" transform="Add" property="text"/>
</bindings>
</label>
<label id="lblName">
<bindings>
<binding dataPath="Name" property="text"/>
</bindings>
</label>
<label id="lblEmail">
<bindings>
<binding dataPath="Email" property="text"/>
</bindings>
</label>
</template>
</itemTemplate>
<emptyTemplate>
<template layoutElement="myList_emptyTemplate"/>
</emptyTemplate>
</listView>
第四部分: DataNavigator控件以及命令按鈕。注意到這里我們有四個(gè)按鈕,每一個(gè)都有不同的commandName屬性,也分別對應(yīng)著DataNavigator對DataView的一種操作。同時(shí)這些按鈕的parent屬性都設(shè)置成了這個(gè)DataNavigator對象。
<dataNavigator id="pageNavigator" dataView="view"/>
<button id="btnFirstPage" parent="pageNavigator" command="FirstPage" />
<button id="btnPrevPage" parent="pageNavigator" command="PreviousPage">
<bindings>
<binding property="enabled" dataPath="hasPreviousPage"/>
</bindings>
</button>
<button id="btnNextPage" parent="pageNavigator" command="NextPage">
<bindings>
<binding property="enabled" dataPath="hasNextPage"/>
</bindings>
</button>
<button id="btnLastPage" parent="pageNavigator" command="LastPage" />
第五部分:兩個(gè)Label,分別顯示頁面總數(shù)以及當(dāng)前頁的序號。
<label id="lblPageNumber">
<bindings>
<binding dataContext="view" property="text" dataPath="pageIndex" transform="Add"/>
</bindings>
</label>
<label id="lblPageCount">
<bindings>
<binding dataContext="view" property="text" dataPath="pageCount"/>
</bindings>
</label>
OK,在瀏覽器中測試一下:
新聞熱點(diǎn)
疑難解答
圖片精選