Farseer.net轻量级开源框架 中级篇:数据绑定
上一篇:Farseer.net轻量级开源框架 中级篇: DbFactory数据工厂
下一篇:Farseer.net轻量级开源框架 中级篇: 自定义配置文件
这篇文章讲的内容,大家应该会比较感兴趣。讲述如何将自己从数据库获取的数据展现在表现层,当然这篇讲的主要是Asp.Net的服务器控件方面的数据绑定。
说到ListControl,大家可能会陌生,但说:DropDownList、CheckBoxList、RadioButtonList大家可能会就比较熟悉了,这些控件实质上是继承了ListControl类的。
当然,还有大家熟悉的Repeater控件(我比较倾向这个数据列表控件,主要是灵活)
基本上控件的绑定都是非常简单的,如下:
"控件的实例名称".Bind(........);
Bind方法是扩展方法,需要引用空间:FS.Extend;
在最早的讲述增删改查的演示教程中。已经有了Repeater、RadioButtonList的绑定例子,我们再次回顾下:
// 绑定 性别 枚举 到控件中
// 通过对枚举的 [Display(Name = "男士")] 设置来显示中文
// 对于DropDownList、CheckBoxList、RadioButtonList、Repeater 的绑定操作是完全一样的哦
// 当然不仅仅是绑定枚举、包括你的List<User> 都可以。对于实体的中文显示,默认是Caption字段
hlGenderType.Bind(typeof(Users.eumGenderType));
hl2GenderType.Bind(typeof(Users.eumGenderType)); // 数据分页、并绑定到Repeater中
// 这里的分页是真实的数据库先进行分页获取数据,然后再在Repeater中进行实现 上一页、下一页的操作显示
// rptList 是FS:Repeater控件 继承自原生的Repeater控件的基础上,实现上下页的菜单显示。
// ToList 传入rptList。实际是告诉ToList 我要从数据库获取第几页的数据,同时把数据库的Count总数,设置到Repeater中
rptList.Bind(Users.Data.ToList(rptList));
语法上的调用是非常方便的,当然这些都是Farseer.Net的初衷,快速开发!
有了这个开头,现在我们演示下不同的数据、不同的控件是如何绑定的。
/// <summary>
/// Eume绑定到WebControl
/// </summary>
/// <param name="control">要绑定的ddl</param>
/// <param name="eumType">枚举的Type</param>
/// <param name="defShowText">第一行要显示的文字</param>
/// <param name="defShowValue">第一行要显示的值</param>
/// <param name="selectedValue">默认选择值</param>
public static void Bind(this ListControl control, Type eumType, object selectedValue = null, string defShowText = null, object defShowValue = null)
/// <summary>
/// WinForm绑定
/// </summary>
/// <param name="control">控件</param>
/// <param name="eumType">枚举类型</param>
public static void Bind(this System.Windows.Forms.ListControl control, Type eumType)
/// <summary>
/// WinForm绑定
/// </summary>
/// <param name="control">控件</param>
/// <param name="eumType">枚举类型</param>
public static void Bind(this DataGridViewComboBoxColumn control, Type eumType)
/// <summary>
/// 绑定枚举
/// </summary>
/// <param name="rpt">Repeater</param>
/// <param name="eumType">枚举</param>
public static void Bind(this Repeater rpt, Type eumType)
主要是针对以上4种控件类型,实现了绑定。大家知道枚举元素的语法定义是 Name=Value
Name通常是英文的变量名称、Value是数字类型的值。但是并没有中文的显示的地方。那么回头看看我们的中文是放到哪的:
/// <summary>
/// 性别类型
/// </summary>
public enum eumGenderType
{
/// <summary>
/// 男士
/// </summary>
[Display(Name = "男士")]
Man = , /// <summary>
/// 女士
/// </summary>
[Display(Name = "女士")]
Woman
}
没错,跟实体的字段映射是一样的,也是:Display特性。那么在绑定的时候,框架会加载这个中文(这里也使用了缓存哦)
这样,我们在控件显示的时候,就会把这个值的中文显示出来:
我们在看看它的服务器控件又是如何的:
<asp:RadioButtonList ID="hlGenderType" runat="server" RepeatDirection="Horizontal"></asp:RadioButtonList>
它的HTML效果的:
<tr>
<td>
<input id="hlGenderType_0" type="radio" name="hlGenderType" value="0" checked="checked" /><label for="hlGenderType_0">男士</label></td>
<td>
<input id="hlGenderType_1" type="radio" name="hlGenderType" value="1" /><label for="hlGenderType_1">女士</label></td>
</tr>
.Net 后台
hlGenderType.Bind(typeof(Users.eumGenderType));
/// <summary>
/// string[]绑定到WebControl
/// </summary>
/// <param name="control">要绑定的ddl</param>
/// <param name="trueCaption">值为是的提示</param>
/// <param name="falseCaption">值为不是的提示</param>
/// <param name="NoSelectCaption">未选择的提示</param>
public static void Bind(this ListControl control, string trueCaption = "是", string falseCaption = "否", string NoSelectCaption = "")
/// <summary>
/// IEnumerable绑定到DataGridView
/// </summary>
/// <param name="dgv">DataGridView</param>
/// <param name="lst">List列表</param>
public static void Bind<T>(this DataGridView dgv, List<T> lst) /// <summary>
/// IEnumerable绑定到DataGridView
/// </summary>
/// <param name="dgv">DataGridView</param>
/// <param name="lst">List列表</param>
public static void Bind<T>(this DataGridView dgv, BindingList<T> lst, Action<object, ListChangedEventArgs> act = null) /// <summary>
/// 绑定到DropDownList
/// </summary>
/// <param name="ddl">要绑定的ddl控件</param>
/// <param name="lstInfo">要进行绑定的列表</param>
/// <param name="selectedValue">默认选则值</param>
/// <param name="RemoveID">不加载的节点(包括子节点)</param>
public static void Bind(this DropDownList ddl, List<ModelCateInfo> lstInfo, object selectedValue = null, int RemoveID = -) /// <summary>
/// IEnumerable绑定到Repeater
/// </summary>
/// <param name="rpt">Repeater</param>
/// <param name="lst">List列表</param>
public static void Bind(this Repeater rpt, IEnumerable lst) /// <summary>
/// IEnumerable绑定到Repeater
/// </summary>
/// <param name="rpt">QynRepeater</param>
/// <param name="recordCount">记录总数</param>
/// <param name="lst">IEnumerable</param>
public static void Bind(this UI.Repeater rpt, IEnumerable lst, int recordCount = -)
看下展示效果:
HTML:
<table class="auto-style1" style="width: 800px">
<tr>
<td>系统编号</td>
<td>名称</td>
<td>密码</td>
<td>性别</td>
<td>登陆次数</td>
<td>登陆IP</td>
<td>操作</td>
</tr>
<FS:Repeater ID="rptList" runat="server">
<ItemTemplate>
<tr>
<td><%# ((Users)Container.DataItem).ID%></td>
<td><%# ((Users)Container.DataItem).UserName%></td>
<td><%# ((Users)Container.DataItem).PassWord%></td>
<td><%# ((Users)Container.DataItem).GenderType.GetName()%></td>
<td><%# ((Users)Container.DataItem).LoginCount%></td>
<td><%# ((Users)Container.DataItem).LoginIP%></td>
<td><a href="?oper=update&ID=<%# ((Users)Container.DataItem).ID%>">修改</a>
<a href="?oper=del&ID=<%# ((Users)Container.DataItem).ID%>">删除</a>
</td>
</tr>
</ItemTemplate>
<PaginationHtml><tr class="tdbg" align="center" style="height: 28px;"><td colspan="12"><Pagination /></td></tr></PaginationHtml>
</FS:Repeater>
</table>
这里用到的是框架的FS:Repeater(加了分页功能)、系统原生的Repeater也是一样的效果。
再看看后台.net:
// 数据分页、并绑定到Repeater中
// 这里的分页是真实的数据库先进行分页获取数据,然后再在Repeater中进行实现 上一页、下一页的操作显示
// rptList 是FS:Repeater控件 继承自原生的Repeater控件的基础上,实现上下页的菜单显示。
// ToList 传入rptList。实际是告诉ToList 我要从数据库获取第几页的数据,同时把数据库的Count总数,设置到Repeater中
rptList.Bind(Users.Data.ToList(rptList));
Users.Data.ToList(rptList) 返回的是List<Users>类型。参数里把Repeater传了进去。是因为Repeater控件带了分页的属性,比如每页显示大小、总记录数。通过这种重载版本,就不需要再对ToList分页的时候,再次设置,直接调用Repeater的属性。
还是贴下代码吧,看看这个重载做了什么:
/// <summary>
/// 通用的分页方法(多条件)
/// </summary>
/// <param name="rpt">Repeater带分页控件</param>
/// <param name="db">可传入事务的db</param>
public List<TInfo> ToList(Repeater rpt, DbExecutor db = null)
{
int recordCount;
var lst = ToList(out recordCount, rpt.PageSize, rpt.PageIndex, db);
rpt.PageCount = recordCount; return lst;
}
实际上还是调用了ToList的分页重载版本。
对于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个整型的字段就可以了。
var enumValue = ;
enumValue |= 选中的值(int类型)
"CheckBox控件".Selected = (val & value) == val;
// value 是数据库存的权限值
// val是CheckBox控件的值
// 进行与运算比较
下面是比较常见的List<T>值的赋值与获取,这个就是用List方式进行传入的,上面的枚举的位运算可能大家不常用。
/// <summary>
/// 获取ListBox的值
/// </summary>
/// <param name="control">ListControl控件</param>
public static List<T> GetValue<T>(this ListControl control) /// <summary>
/// 设置ListBox的值
/// </summary>
/// <param name="control">ListControl控件</param>
/// <param name="lst">要设置的值</param>
public static void SetValue<T>(this ListControl control, List<T> lst)
/// <summary>
/// 插入项
/// </summary>
/// <param name="control">control控件</param>
/// <param name="text">显示的名称</param>
/// <param name="value">保存的值</param>
/// <param name="index">插入的项索引</param>
public static void InsertItem(this ListControl control, object value, string text = "请选择", int index = ) /// <summary>
/// 选择项
/// </summary>
/// <param name="control">ListControl控件</param>
/// <param name="selectedValue">选择项</param>
public static void SelectedItems(this ListControl control, object selectedValue)
这个是对控件的插入新项、或者选择某一选的扩展方法。.net 原生的方法,如果在做选择的时候,这个项不存在,是会报异常的。而这个扩展则对这个进行了判断。
上一篇:Farseer.net轻量级开源框架 中级篇: DbFactory数据工厂
下一篇:Farseer.net轻量级开源框架 中级篇: 自定义配置文件
QQ群:116228666 (Farseer.net开源框架交流) 请注明:Farseer.Net
Farseer.Net是一款ORM框架 + 常用工具 + 扩展集合。
Farseer 意为:先知、预言家 通常在某些场合时,提供计谋、策略。也希望该框架能给大家提供最大化的便捷。
ORM:其英文全称是:Object(对象) Relational(关系) Mapping(映射)
Farseer.Net的目标是:快速上手、快速开发、简单方便。
new User { ID = , Name = "张三" }.Insert()
Farseer.net轻量级开源框架 中级篇:数据绑定的更多相关文章
- Farseer.net轻量级开源框架 中级篇:DbFactory数据工厂
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: 执行SQL语句 下一篇:Farseer.net轻量级开源框架 中级篇: 数据绑定 ...
- Farseer.net轻量级开源框架 中级篇:自定义配置文件
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: 数据绑定 下一篇:Farseer.net轻量级开源框架 中级篇: 动态数据库访问 ...
- Farseer.net轻量级开源框架 中级篇:Cookies、Session、Request
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: 探究ORM(Mapping) 下一篇:Farseer.net轻量级开源框架 中级篇 ...
- Farseer.net轻量级开源框架 中级篇:UrlRewriter 地址重写
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: Cookies.Session.Request 下一篇:Farseer.net轻量 ...
- Farseer.net轻量级开源框架 中级篇:常用的扩展方法
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: BasePage.BaseController.BaseHandler.BaseM ...
- Farseer.net轻量级开源框架 中级篇:BasePage、BaseController、BaseHandler、BaseMasterPage、BaseControls基类使用
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: UrlRewriter 地址重写 下一篇:Farseer.net轻量级开源框架 中 ...
- Farseer.net轻量级开源框架 中级篇:探究ORM(Mapping)
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: SQL执行报告 下一篇:Farseer.net轻量级开源框架 中级篇: Cooki ...
- Farseer.net轻量级开源框架 中级篇:SQL执行报告
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: 数据库切换 下一篇:Farseer.net轻量级开源框架 中级篇: 探究ORM(M ...
- Farseer.net轻量级开源框架 中级篇:事务的使用
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: Where条件的终极使用 下一篇:Farseer.net轻量级开源框架 中级篇: ...
随机推荐
- DELL T110II Server如何通过RAID 级别迁移的方式在OMSA下实现磁盘阵列扩容?
目录: RAID 转移规则说明 操作步骤 本文介绍了 通过RAID 级别转换来实现扩容的方法注意:本文相关RAID的操作,仅供在测试环境里学习和理解戴尔PowerEdge服务器RAID控制卡的功能和使 ...
- Outlook2010 没有Exchange Server,怎么自动回复邮件?
步骤 1:创建邮件模板 单击“开始”>“新建邮件”. 在邮件正文中,键入要作为自动答复发送的邮件. 在邮件窗口中,单击“文件”>“另存为”. 在“另存为”对话框中的“保存类型”列表中,单击 ...
- scikit-learn:3.3. Model evaluation: quantifying the quality of predictions
參考:http://scikit-learn.org/stable/modules/model_evaluation.html#scoring-parameter 三种方法评估模型的预測质量: Est ...
- JavaScript 获得代码行号和脚本文件名
如果你使用的是 V8 引擎,Chrome 和 Node.js 所用的,那么你可以利用 JavaScriptStackTraceApi 来获得行号信息,有两个 API: Error.captureSta ...
- IEDA-maven引用本地jia包
简单说下为啥用maven引用本地jar包:当在pom文件中配置需要引用了jar的坐标,但是maven引用不了(原因未知情况下),这种情况下就需要找开发提供相关依赖的的jar文件打成一个jar包发送过来 ...
- HDU5465/BestCoder Round #56 (div.2) 二维树状数组
Clarke and puzzle 问题描述 克拉克是一名人格分裂患者.某一天,有两个克拉克(aa和bb)在玩一个方格游戏. 这个方格是一个n*mn∗m的矩阵,每个格子里有一个数c_{i, j}ci ...
- TypeError: expected bytes-like object, not str
报错内容:TypeError: expected bytes-like object, not str 例: a = base64.b64encode(temp) 改为: a = base64.b64 ...
- 洛谷 P1965 转圈游戏 —— 快速幂
题目:https://www.luogu.org/problemnew/show/P1965 居然真的就只是 ( x + m * 10k % n ) % n 代码如下: #include<ios ...
- Python Matplotlib模块--pyplot
#-*- coding: utf- -*- ''' numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=No ...
- Linux 系统管理命令 - uptime - 显示系统的运行时间及负载
命令详解 重要星级: ★★★☆☆ 功能说明: uptime 命令可以输出当前系统时间.系统开机到现在的运行时间.目前有多少用户在线和系统平均负载等信息 语法格式: uptime 说明: 直接执行 up ...