CYQ.Data componentsCYQ.Data support multi-database application [Txt,Xml,Access, MSSQL, Oracle,SQLite,MySql], help easily and quickly to develop your project |
CYQ.Data 快速开发之UI(赋值、取值、绑定)原理
Principles |
|
|
| #TopicOwner |
CYQ.Data 的使用操作方式,已经有相关文章介绍了,就不再介绍了。 本节就讲一下实现原理,具体源码,可直接下载开源的V4.0可以学习。 下载地址:http://www.cyqdata.com/download/article-detail-426 CYQ.Data 支持的UI:WebForm和Winform 及实现原理:
涉及的内部2个类和一个接口:
MActionUI: 主要是对单行数据的操作,将单行的数据赋值到WebForm的服务器控件或Winform的控件。
其功能就是对UI控件单个取值和赋值,而新增的SetAll,可以批量把值赋值控件。 示例代码: using (MAction action = new MAction("Users"))
{ if (action.Fill(id)) { action.SetToAll(this); } }
就这么一行SetToAll(this),就秒杀了以前的N多的类似以下的代码: txtID.Text = XXX.值1;
txtUserName.Text = XXX.值2; txtFullName.Text = XXX.值3; txtEmailName.Text = XXX.值4; txtPassword.Text = XXX.值5; ....省略N个.....
而且带来的好处是,就算添加或减少字段显示项,只要把页面的控件去掉即可,并不即改动代码。
那这个功能实现原理是什么? 其实就是“this",等于传进了当前Page,然后只要遍历Page的子控件,根据约定的“三个字母前缀”+字段名,就可以循环设置值了。
MBindUI: 主要是对多数据的操作,将多行的数据绑定到WebForm或Winform的GridView,Repleater,DropDownList等列表控件。
其功能其实很简单,就是绑定列表控件,看以下以下代码,大致就能理解了: public static void Bind(object ct, object source)
{ if (ct is GridView) { ((GridView)ct).DataSource = source; ((GridView)ct).DataBind(); } else if (ct is Repeater) { ((Repeater)ct).DataSource = source; ((Repeater)ct).DataBind(); } else if (ct is DataList) { ((DataList)ct).DataSource = source; ((DataList)ct).DataBind(); } else if (ct is DataGrid) { ((DataGrid)ct).DataSource = source; ((DataGrid)ct).DataBind(); } else if (ct is Win.DataGrid) { ((DataGrid)ct).DataSource = source; } else if (ct is Win.DataGridView) { ((System.Windows.Forms.DataGridView)ct).DataSource = source; } else if (ct is BaseDataList)//基类处理 { ((BaseDataList)ct).DataSource = source; ((BaseDataList)ct).DataBind(); } }
通过多个分支,设置数据源的值,而经典的使用方式就是: using (MAction action = new MAction("Users"))
{ action.Select().Bind(gvUsers); }
当然,这并不是最佳性能的使用方式,因为列表控件的绑定,最好是在数据库链接关闭后再绑定,最佳性能写法应该下: MDataTable dt;
using (MAction action = new MAction("Users")) { dt = action.Select(); } dt.Bind(gvUsers);
IUIValue: 这是一个支持自定义控件或第三方控件的接口,只要控件实现接口,也可以使用MActionUI的功能。 三个属性,控件是否启,控件ID,控件的值,这个一般只有自己编写的自定义控件才会使用到。
UI这一块差不多就讲到这里了,实现还算简单,但思路却是多年经验步步优化累积而来。
传统非UI开发模式:
除开WebForm,事实还有很多种开发模式,但万变不离其宗的是,本质是html元素通过GET、POST等方式提交与服务端交互。
而这种交互中间,最常见的就是js,而交互的格式,json是深受喜爱的。
所以CYQ.Data 也考虑到这种开发模式的快速开发方式,通过GetFromJson与ToJson功能,实现简单后端编码: 示例代码如: string result;
using (MAction action = new MAction("Users")) { if (action.Fill(id)) { result=action.Data.ToJson(); } } Response.Write(result); 通过返回Json到客户端,由客户端js解析并显示。
同样的返过来批量取值: bool result;
using (MAction action = new MAction("Users")) { action.GetFromJson(Request["json"]); result = action.Insert(); } Response.Write(result); 由客户端js提交json格式的数据,而后台固定自动解析,入库,相当方便。
OK,本节就介绍到这里,谢谢观赏。
|
游客[注册][124.206.31.*]2013/6/19 11:13:17 | #1 | |
好文,找了好半天 |
Post Comment
Bulletin
Article Search
Categories
- Platform for dynamic (20)
- Feedback (9)
- Guide (33)
- Principles (19)
- Project-Case (8)
- Business & Buy (2)
- Technology exchange (45)
New Article
- CYQ.Data Components Getting Started Guide [Part 5]-[MProc Execute Stored Procedures or SQL]
- CYQ.Data Components Getting Started Guide [Part 4]-[MAction Insert Delete Update]
- CYQ.Data Components Getting Started Guide [Part 3]-[MAction Get And Set Value]
- CYQ.Data Components Getting Started Guide [Part 2]-[MAction Data Query- Fill And Select]
- CYQ.Data Components Getting Started Guide [Part 1]
New Comment
- When some one searches for his necessary thing, therefore he/she wishes to be available that in detail, so that thing is maintained over here.
- This is my first time pay a quick visit at here and i am in fact happy to read everthing at alone place.
- I truly appreciate this blog article.Really thank you! Cool.
- please pay a visit to the web sites we follow, like this one particular, as it represents our picks in the web
- Really enjoyed this post.Really thank you!
- Really enjoyed this article.Really looking forward to read more. Great.
- poker bonuses What are the norms of copyright of web content? How as it different from Patent?
- Wow! Thank you! I permanently needed to write on my blog something like that. Can I implement a fragment of your post to my site?
- This website was how do I say it? Relevant!! Finally I ave found something that helped me. Cheers!
- I was reading through some of your content on this internet site and I believe this web site is very informative ! Continue posting.