ASP.NET控件开发基础(18)
http://tech.ddvip.com 2007年09月02日 社区交流
内容摘要:本篇继续上篇的讨论,可能大家已经在使用asp.net2.0了,DataSource属性不再使用,而是跟数据源控件搭配使用.现在讨论的绑定技术都是基于1.1版本,先熟悉一下,本质上是一样的,这样一步步的学习.对以后绝对有帮助.因为当你使用数据源控件,只需要设置一个DataSourceID,方便的同时你是否知道数据源控件帮你做了什么事情,如果你想觉的够用了,可以不用了解,但我相信你一定会有需求.
其中最大的变化在于这里,因为还需要支持DataSet,DataSourceHelper类负责解析传入的数据源DataSouce进行解析
if (useDataSource)
{
//解析DataSource
ds = (IEnumerable)DataSourceHelper.ResolveDataSource(DataSource,
DataMember);
}
下面我们来重点看DataSourceHelper类
DataSourceHelper类可谓是这一篇的重头戏,关键就在于这里的理解.这里搞明白了,才算是明白.一起来看吧
1 /**//// <summary>
2 /// 一个解析DataSource的辅助类
3 /// </summary>
4 public class DataSourceHelper
5 {
6 public static object ResolveDataSource(object dataSource, string dataMember)
7 {
8 如果数据源为空,则返回空值#region 如果数据源为空,则返回空值
9
10 if (dataSource == null)
11 return null;
12
13 #endregion
14
15 如果数据源不为空,且为IEnumerable类型,则返回IEnumerable#region 如果数据源不为空,且为IEnumerable类型,则返回IEnumerable
16
17 if (dataSource is IEnumerable)
18 {
19 return (IEnumerable)dataSource;
20 }
21
22 #endregion
23
24 如果数据源不为空,且为IListSource类型,则返回IListSource#region 如果数据源不为空,且为IListSource类型,则返回IListSource
25
26 else if (dataSource is IListSource)
27 {
28 IList list = null;
29 IListSource listSource = (IListSource)dataSource;
30 list = listSource.GetList();
31 判断是否为IList对象集合的值#region 判断是否为IList对象集合的值
32 if (listSource.ContainsListCollection)
33 {
34 //提供发现可绑定列表架构的功能,其中可用于绑定的属性不同于要绑定到的对象的公共属性
35 ITypedList typedList = (ITypedList)list;
36 //返回表示用于绑定数据的每项上属性集合
37 PropertyDescriptorCollection propDescCol =
38 typedList.GetItemProperties(new PropertyDescriptor[0]); //was (null)
39
40 //如果属性说明符数目为0
41 if (propDescCol.Count == 0)
42 throw new Exception("ListSource without DataMembers");
43
44 PropertyDescriptor propDesc = null;
45
46 判断dataMember字符数给propDesc赋值#region 判断dataMember字符数给propDesc赋值
47 //获取属性描述符
48 //若不指定dataMember属性则获取默认数据成员
49 if ((dataMember == null) || (dataMember.Length < 1))
50 {
51 propDesc = propDescCol[0];
52 }
53 else
54 //尝试在属性集合众寻找数据成员
55 propDesc = propDescCol.Find(dataMember, true);
56
57 #endregion
58
59 if (propDesc == null)
60 throw new Exception("ListSource missing DataMember");
61
62 object listitem = list[0];
63
64 //获取DataTable
65 object member = propDesc.GetValue(listitem);
66
67 if ((member == null) || !(member is IEnumerable))
68 throw new Exception("ListSource missing DataMember");
69
70 return (IEnumerable)member;
71 }
72 else
73 //若不包含Ilist集合,则直接返回
74 return (IEnumerable)list; //robcamer added (IEnumerable)
75
76 #endregion
77 }
78
79 #endregion
80 return null;
81
82 }
83 }
责编:豆豆技术应用
- asp.net 视频教程
- asp.net 数据库编程
- asp.net 入门教程
- ado.net 教程
- asp.net 基础讲座
- asp.net ajax 教程
- asp.net ajax 入门系列
- asp.net 控件开发基础
- asp.net 2.0 服务器控件
- asp.net 2.0 教程
- asp.net 控件开发
- asp.net 类
- asp.net 分页
- asp.net 页面缓存
- asp.net 常见问题解决
- asp.net 2.0 母版页
- asp.net SQL Server
- asp.net 错误
- asp.net 事件
- asp.net 组件
- asp.net 性能
- asp.net 文件上传
- 更多asp.net专题……