码迷,mamicode.com
首页 > Web开发 > 详细

Farseer.net轻量级开源框架 中级篇:数据绑定

时间:2014-11-07 14:39:54      阅读:337      评论:0      收藏:0      [点我收藏+]

标签:winform   datagridview   分页控件   style   blog   http   io   color   ar   

导航

目   录:Farseer.net轻量级开源框架 目录

上一篇:Farseer.net轻量级开源框架 中级篇: DbFactory数据工厂

下一篇:Farseer.net轻量级开源框架 中级篇: 自定义配置文件

  这篇文章讲的内容,大家应该会比较感兴趣。讲述如何将自己从数据库获取的数据展现在表现层,当然这篇讲的主要是Asp.Net的服务器控件方面的数据绑定。

控件的绑定

  说到ListControl,大家可能会陌生,但说:DropDownList、CheckBoxList、RadioButtonList大家可能会就比较熟悉了,这些控件实质上是继承了ListControl类的。

  当然,还有大家熟悉的Repeater控件(我比较倾向这个数据列表控件,主要是灵活)

  基本上控件的绑定都是非常简单的,如下:

1 "控件的实例名称".Bind(........);

  Bind方法是扩展方法,需要引用空间:FS.Extend;

  在最早的讲述增删改查的演示教程中。已经有了Repeater、RadioButtonList的绑定例子,我们再次回顾下:

 1             // 绑定 性别 枚举 到控件中
 2             // 通过对枚举的 [Display(Name = "男士")] 设置来显示中文 
 3             // 对于DropDownList、CheckBoxList、RadioButtonList、Repeater 的绑定操作是完全一样的哦
 4             // 当然不仅仅是绑定枚举、包括你的List<User> 都可以。对于实体的中文显示,默认是Caption字段
 5             hlGenderType.Bind(typeof(Users.eumGenderType));
 6             hl2GenderType.Bind(typeof(Users.eumGenderType));
 7 
 8            // 数据分页、并绑定到Repeater中
 9            // 这里的分页是真实的数据库先进行分页获取数据,然后再在Repeater中进行实现 上一页、下一页的操作显示
10            // rptList 是FS:Repeater控件 继承自原生的Repeater控件的基础上,实现上下页的菜单显示。 
11            // ToList 传入rptList。实际是告诉ToList 我要从数据库获取第几页的数据,同时把数据库的Count总数,设置到Repeater中
12            rptList.Bind(Users.Data.ToList(rptList));

  语法上的调用是非常方便的,当然这些都是Farseer.Net的初衷,快速开发!

  有了这个开头,现在我们演示下不同的数据、不同的控件是如何绑定的。

枚举的绑定
 1         /// <summary>
 2         ///     Eume绑定到WebControl
 3         /// </summary>
 4         /// <param name="control">要绑定的ddl</param>
 5         /// <param name="eumType">枚举的Type</param>
 6         /// <param name="defShowText">第一行要显示的文字</param>
 7         /// <param name="defShowValue">第一行要显示的值</param>
 8         /// <param name="selectedValue">默认选择值</param>
 9         public static void Bind(this ListControl control, Type eumType, object selectedValue = null, string defShowText = null, object defShowValue = null)
1         /// <summary>
2         ///     WinForm绑定
3         /// </summary>
4         /// <param name="control">控件</param>
5         /// <param name="eumType">枚举类型</param>
6         public static void Bind(this System.Windows.Forms.ListControl control, Type eumType)
1         /// <summary>
2         ///     WinForm绑定
3         /// </summary>
4         /// <param name="control">控件</param>
5         /// <param name="eumType">枚举类型</param>
6         public static void Bind(this DataGridViewComboBoxColumn control, Type eumType)
1         /// <summary>
2         ///     绑定枚举
3         /// </summary>
4         /// <param name="rpt">Repeater</param>
5         /// <param name="eumType">枚举</param>
6         public static void Bind(this Repeater rpt, Type eumType)

  主要是针对以上4种控件类型,实现了绑定。大家知道枚举元素的语法定义是 Name=Value

  Name通常是英文的变量名称、Value是数字类型的值。但是并没有中文的显示的地方。那么回头看看我们的中文是放到哪的:

 1         /// <summary>
 2         /// 性别类型
 3         /// </summary>
 4         public enum eumGenderType
 5         {
 6             /// <summary>
 7             /// 男士
 8             /// </summary>
 9             [Display(Name = "男士")]
10             Man = 0,
11 
12             /// <summary>
13             /// 女士
14             /// </summary>
15             [Display(Name = "女士")]
16             Woman
17         }

  没错,跟实体的字段映射是一样的,也是:Display特性。那么在绑定的时候,框架会加载这个中文(这里也使用了缓存哦)

  这样,我们在控件显示的时候,就会把这个值的中文显示出来:

  bubuko.com,布布扣

  我们在看看它的服务器控件又是如何的:

1 <asp:RadioButtonList ID="hlGenderType" runat="server" RepeatDirection="Horizontal"></asp:RadioButtonList>

  它的HTML效果的:

1 <tr>
2     <td>
3         <input id="hlGenderType_0" type="radio" name="hlGenderType" value="0" checked="checked" /><label for="hlGenderType_0">男士</label></td>
4     <td>
5         <input id="hlGenderType_1" type="radio" name="hlGenderType" value="1" /><label for="hlGenderType_1">女士</label></td>
6 </tr>

  .Net 后台

1 hlGenderType.Bind(typeof(Users.eumGenderType)); 
布尔值的绑定
1         /// <summary>
2         ///     string[]绑定到WebControl
3         /// </summary>
4         /// <param name="control">要绑定的ddl</param>
5         /// <param name="trueCaption">值为是的提示</param>
6         /// <param name="falseCaption">值为不是的提示</param>
7         /// <param name="NoSelectCaption">未选择的提示</param>
8         public static void Bind(this ListControl control, string trueCaption = "", string falseCaption = "", string NoSelectCaption = "")
List<实体>的绑定
 1         /// <summary>
 2         ///     IEnumerable绑定到DataGridView
 3         /// </summary>
 4         /// <param name="dgv">DataGridView</param>
 5         /// <param name="lst">List列表</param>
 6         public static void Bind<T>(this DataGridView dgv, List<T> lst)
 7 
 8         /// <summary>
 9         ///     IEnumerable绑定到DataGridView
10         /// </summary>
11         /// <param name="dgv">DataGridView</param>
12         /// <param name="lst">List列表</param>
13         public static void Bind<T>(this DataGridView dgv, BindingList<T> lst, Action<object, ListChangedEventArgs> act = null)
14 
15         /// <summary>
16         ///     绑定到DropDownList
17         /// </summary>
18         /// <param name="ddl">要绑定的ddl控件</param>
19         /// <param name="lstInfo">要进行绑定的列表</param>
20         /// <param name="selectedValue">默认选则值</param>
21         /// <param name="RemoveID">不加载的节点(包括子节点)</param>
22         public static void Bind(this DropDownList ddl, List<ModelCateInfo> lstInfo, object selectedValue = null, int RemoveID = -1)
23 
24         /// <summary>
25         ///     IEnumerable绑定到Repeater
26         /// </summary>
27         /// <param name="rpt">Repeater</param>
28         /// <param name="lst">List列表</param>
29         public static void Bind(this Repeater rpt, IEnumerable lst)
30 
31         /// <summary>
32         ///     IEnumerable绑定到Repeater
33         /// </summary>
34         /// <param name="rpt">QynRepeater</param>
35         /// <param name="recordCount">记录总数</param>
36         /// <param name="lst">IEnumerable</param>
37         public static void Bind(this UI.Repeater rpt, IEnumerable lst, int recordCount = -1)

  看下展示效果:

bubuko.com,布布扣

  HTML:

 1                 <table class="auto-style1" style="width: 800px">
 2                     <tr>
 3                         <td>系统编号</td>
 4                         <td>名称</td>
 5                         <td>密码</td>
 6                         <td>性别</td>
 7                         <td>登陆次数</td>
 8                         <td>登陆IP</td>
 9                         <td>操作</td>
10                     </tr>
11                     <FS:Repeater ID="rptList" runat="server">
12                         <ItemTemplate>
13                             <tr>
14                                 <td><%# ((Users)Container.DataItem).ID%></td>
15                                 <td><%# ((Users)Container.DataItem).UserName%></td>
16                                 <td><%# ((Users)Container.DataItem).PassWord%></td>
17                                 <td><%# ((Users)Container.DataItem).GenderType.GetName()%></td>
18                                 <td><%# ((Users)Container.DataItem).LoginCount%></td>
19                                 <td><%# ((Users)Container.DataItem).LoginIP%></td>
20                                 <td><a href="?oper=update&ID=<%# ((Users)Container.DataItem).ID%>">修改</a>
21                                     <a href="?oper=del&ID=<%# ((Users)Container.DataItem).ID%>">删除</a>
22                                 </td>
23                             </tr>
24                         </ItemTemplate>
25                         <PaginationHtml><tr class="tdbg" align="center" style="height: 28px;"><td colspan="12"><Pagination /></td></tr></PaginationHtml>
26                     </FS:Repeater>
27                 </table>

  这里用到的是框架的FS:Repeater(加了分页功能)、系统原生的Repeater也是一样的效果。

  再看看后台.net:

1         // 数据分页、并绑定到Repeater中
2         // 这里的分页是真实的数据库先进行分页获取数据,然后再在Repeater中进行实现 上一页、下一页的操作显示
3         // rptList 是FS:Repeater控件 继承自原生的Repeater控件的基础上,实现上下页的菜单显示。 
4         // ToList 传入rptList。实际是告诉ToList 我要从数据库获取第几页的数据,同时把数据库的Count总数,设置到Repeater中
5         rptList.Bind(Users.Data.ToList(rptList));

  Users.Data.ToList(rptList) 返回的是List<Users>类型。参数里把Repeater传了进去。是因为Repeater控件带了分页的属性,比如每页显示大小、总记录数。通过这种重载版本,就不需要再对ToList分页的时候,再次设置,直接调用Repeater的属性。

  还是贴下代码吧,看看这个重载做了什么:

 1         /// <summary>
 2         ///     通用的分页方法(多条件)
 3         /// </summary>
 4         /// <param name="rpt">Repeater带分页控件</param>
 5         /// <param name="db">可传入事务的db</param>
 6         public List<TInfo> ToList(Repeater rpt, DbExecutor db = null)
 7         {
 8             int recordCount;
 9             var lst = ToList(out recordCount, rpt.PageSize, rpt.PageIndex, db);
10             rpt.PageCount = recordCount;
11 
12             return lst;
13         }

  实际上还是调用了ToList的分页重载版本。

获取ListControl值

   对于DropDownList、RadioButtonList来说,其选中的值都是只有一个的。但是CheckBoxList是复选列表。即我们要对其进行赋值(选中)、或者获取已选中的值时。额外提供了一些扩展方法:

        /// <summary>
        ///     获取CheckBoxList中,选中后的枚举值
        /// </summary>
        /// <param name="control">CheckBoxList控件</param>
        /// <param name="value">设置的值</param>
        public static void SetValue(this CheckBoxList control, int value)

        /// <summary>
        ///     获取CheckBoxList中,选中后的枚举值
        /// </summary>
        /// <param name="control">CheckBoxList控件</param>
        public static int GetValue(this CheckBoxList control)

  这个单独拿出来说。读者会奇怪,这里怎么是返回int?或者,传入的是int呢?

  这里,我们用了位运算。关于权限这一块,有时候,我们会用枚举的位运算来存储权限值。好处是数据库只需要1个整型的字段就可以了。

1 var enumValue = 0;
2 enumValue |= 选中的值(int类型)
1 "CheckBox控件".Selected = (val & value) == val;
2 // value 是数据库存的权限值
3 // val是CheckBox控件的值
4 // 进行与运算比较

  下面是比较常见的List<T>值的赋值与获取,这个就是用List方式进行传入的,上面的枚举的位运算可能大家不常用。

 1         /// <summary>
 2         ///     获取ListBox的值
 3         /// </summary>
 4         /// <param name="control">ListControl控件</param>
 5         public static List<T> GetValue<T>(this ListControl control)
 6 
 7         /// <summary>
 8         ///     设置ListBox的值
 9         /// </summary>
10         /// <param name="control">ListControl控件</param>
11         /// <param name="lst">要设置的值</param>
12         public static void SetValue<T>(this ListControl control, List<T> lst)
其它补充
 1         /// <summary>
 2         ///     插入项
 3         /// </summary>
 4         /// <param name="control">control控件</param>
 5         /// <param name="text">显示的名称</param>
 6         /// <param name="value">保存的值</param>
 7         /// <param name="index">插入的项索引</param>
 8         public static void InsertItem(this ListControl control, object value, string text = "请选择", int index = 0)
 9 
10         /// <summary>
11         ///     选择项
12         /// </summary>
13         /// <param name="control">ListControl控件</param>
14         /// <param name="selectedValue">选择项</param>
15         public static void SelectedItems(this ListControl control, object selectedValue)

  这个是对控件的插入新项、或者选择某一选的扩展方法。.net 原生的方法,如果在做选择的时候,这个项不存在,是会报异常的。而这个扩展则对这个进行了判断。

导航

目   录:Farseer.net轻量级开源框架 目录

上一篇:Farseer.net轻量级开源框架 中级篇: DbFactory数据工厂

下一篇:Farseer.net轻量级开源框架 中级篇: 自定义配置文件

广告时间

QQ群:116228666 (Farseer.net开源框架交流) 请注明:博客园

Farseer.Net是一款ORM框架 + 常用工具 + 扩展集合。

Farseer 意为:先知、预言家 通常在某些场合时,提供计谋、策略。也希望该框架能给大家提供最大化的便捷。

ORM:其英文全称是:Object(对象) Relational(关系) Mapping(映射)

Farseer.Net的目标是:快速上手、快速开发、简单方便。

1 new User { ID = 1, Name = "张三" }.Insert()

Farseer.net轻量级开源框架 中级篇:数据绑定

标签:winform   datagridview   分页控件   style   blog   http   io   color   ar   

原文地址:http://www.cnblogs.com/steden/p/4078521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!