Atlas ItemView控件显示集合中的单个数据
【IT168 技术文档】
有时候我们需要显示给用户列表中某一项的详细信息,例如,在购物程序中的产品详细情况。ASP.NET Atlas ItemView客户端控件为您提供了对这项功能的支持,就像ASP.NET服务器端控件DetailsView一样,但Atlas ItemView控件完全在客户端运行。
ItemView类(ListView类同样,见使用ASP.NET Atlas ListView控件显示列表数据 )继承于Sys.UI.Data.DataControl基类。该基类提供了一些公共的属性,包括:
1, canMoveNext:当前记录后是否有下一条记录。
2,canMovePrevious:当前记录前是否有前一条记录。
3,data:控件包含的数据集合。
4,dataIndex:当前记录的index。
5,dataItem:基于dataIndex的当前的记录。
6,length:记录的条目数。
同时还包括下列方法:
1,addItem:添加一条记录到当前的数据集合中。
2,deleteCurrentItem:删除基于dataIndex的当前记录。
3,moveNext:如果canMoveNext为true,将dataIndex加1,指向下一条记录。
4,movePrevious:如果canMovePrevious为true,将dataIndex减1,指向前一条记录。
请注意所有的以上操作都仅在客户端,也就是说只修改了客户端的数据。所以如果您希望将改变提交到服务器,则需要调用DataSource的相应方法。
ItemView通过继承获得了以上的属性和方法,并且还对基类有如下扩展:
1,itemTemplate:指定项目模版。Atlas可以根据这个模版渲染您的内容。
2,emptyTemplate:指定无数据时的模版。当数据集合为空或者DataSource还在取得数据的过程中时,Atlas会显示这个模版。
以上是ItemView的简要介绍。让我们通过一个例子来熟悉ItemView。这个程序基于Atlas官方发布的示例程序,并适当做了一些简化。
首先暴露一个Web Service以被Atlas使用。
定义item entry类:
class Entry
public class Entry
...{
private string _name;
private string _description;
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 Description
...{
get ...{ return _description; }
set ...{ _description = value; }
}
![]()
public Entry()
...{
_id = -1;
}
![]()
public Entry(int id, string name, string description)
...{
_id = id;
_name = name;
_description = description;
}
}
定义Web Methods。我们需要提供Select,Insert,Update以及Delete方法以期对我们的数据集合进行完整的CRUD操作。注意到我们在初始化数据时使用了System.Threading.Thread.Sleep(2000)来模拟两秒钟的网络延迟,这样可以看到emptyTemplate中的内容。
//Data Service
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MyDataService : 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)
...{
System.Threading.Thread.Sleep(2000);
![]()
_data = new List<Entry>();
_data.Add(new Entry(0, "ListView", "A control to display data using templates."));
_data.Add(new Entry(1, "Window", "A control to display dialogs."));
_data.Add(new Entry(2, "Weather", "A control to display local weather."));
_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 MyDataService.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.Description = updateRow.Description;
break;
}
}
}
}
