【IT168 技术文档】
为您的列表添加排序功能将是非常酷的。一些ASP.NET服务器控件,例如DataGrid,GridView等已经拥有了内建的排序功能。Atlas同样提供了Sys.UI.Data.SortBehavior Behavior(有关Atlas Behavior,请参考:在ASP.NET Atlas中创建自定义的Behavior),让您在客户端即可实现排序功能。
SortBehavior将与DataView控件(请参考:Atlas命名空间Sys.Data下控件介绍——DataView和DataFilter)配合工作,它将通过设定DataView控件的sortDirection属性来实现排序功能。SortBehavior对象有如下属性:
1,dataView:对某个DataView对象的引用,这个SortBehavior将把页面的排序操作应用到其上。您应该总是指定这个属性。
2,sortColumn:DataView中某一列的列名,SortBehavior将根据这个列中的内容来进行排序。您应该总是指定这个属性。
3,sortAscendingCssClass:触发排序的控件的CSS Class(升序时)。
4,sortDescendingCssClass:触发排序的控件的CSS Class(降序时)。
介绍到此为止,我们来看如下的示例程序。
首先我们需要暴露一个Web Service以便Atlas页面使用。该Web Service将返回5个名人(除了我)的简介。下面为代码:
//Data 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 = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class SampleDataService : DataService ...{ static List<Entry> _data; static int _nextId; static object _dataLock = new object(); private static List<Entry> Data ...{ get ...{ if (_data == null) ...{ lock (_dataLock) ...{ if (_data == null) ...{ _data = new List<Entry>(); _data.Add(new Entry(0, "Dflying Chen", "A small piece of cake")); _data.Add(new Entry(1, "Bill Gates", "Chairman & Chief Software Architect")); _data.Add(new Entry(2, "Scott Guthrie", "General Manager")); _data.Add(new Entry(3, "Ray Ozzie", "CTO")); _data.Add(new Entry(4, "Steve Ballmer", "CEO")); _nextId = 3; } } } return _data; } } [DataObjectMethod(DataObjectMethodType.Delete)] public void DeleteRow(int id) ...{ foreach (Entry row in _data) ...{ if (row.Id == id) ...{ lock (_dataLock) ...{ _data.Remove(row); } break; } } } [DataObjectMethod(DataObjectMethodType.Select)] public Entry[] SelectRows() ...{ return SampleDataService.Data.ToArray(); } [DataObjectMethod(DataObjectMethodType.Insert)] public Entry InsertRow(string name, string description) ...{ Entry newRow; lock (_dataLock) ...{ newRow = new Entry(_nextId++, name, description); _data.Add(newRow); } return newRow; } [DataObjectMethod(DataObjectMethodType.Update)] public void UpdateRow(Entry updateRow) ...{ foreach (Entry row in _data) ...{ if (row.Id == updateRow.Id) ...{ row.Name = updateRow.Name; row.Title = updateRow.Title; break; } } } } public class Entry ...{ private string _name; private string _title; 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 Title ...{ get ...{ return _title; } set ...{ _title = value; } } public Entry() ...{ _id = -1; } public Entry(int id, string name, string description) ...{ _id = id; _name = name; _title = description; } }