乐山乐水
.Net与敏捷开发
博客园
首页
新随笔
联系
订阅
管理
5 Posts :: 24 Stories :: 34 Comments :: 2 Trackbacks
与我联系
发短消息
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
留言簿
(2)
给我留言
查看留言
我参与的团队
成都.NET俱乐部(1/1124)
随笔分类
开源项目(3)
敏捷开发
系统架构(2)
随笔档案
2006年6月 (1)
2005年12月 (1)
2005年7月 (1)
2005年1月 (1)
2004年10月 (1)
文章分类
SQL查询(2)
管理文摘(12)
敏捷软件(7)
相册
我的风采
收藏夹
.net Controls(16)
.net tools(17)
.net开发架构(17)
Java tools(2)
open Application(4)
Open Java Project(5)
test(5)
项目管理(2)
.net
.NET Test Driven Development
.net toolbox
51Testing软件测试网
apress book
codeproject
c-sharpcorner
DesignPatterns
ebook
ftponline
InfoQ
ms practices
Oracle .NET Developer Center
regexlib
testdriven.com
theserverside.net
windows beta
常用工具
tools for Java™ development
绿色软件下载
我的项目-basecamp
在线项目管理工具
开源软件
CodePlex
Open Source Software in C#
Open Source Software in Java
oreilly book
企业级开源软件大盘点
朋友站点
ccBoy's(小气的神) BLOG---
home page
软件开发管理
AMT 知识库
项目管理者联盟
最新评论
阅读排行榜
1. 我喜欢的几个控件(2870)
2. 企业库数据层代码生成工具:DataTierGenerator for Enterprise Library(2051)
3. 我在学习的一些.net开源项目(1719)
4. 实体类和显示控件的绑定(753)
5. SOA 分类(392)
评论排行榜
1. 我喜欢的几个控件(12)
2. 企业库数据层代码生成工具:DataTierGenerator for Enterprise Library(8)
3. 我在学习的一些.net开源项目(3)
4. 实体类和显示控件的绑定(0)
5. SOA 分类(0)
实体类和显示控件的绑定
public
class
FormBinding
{
/**/
///
<summary>
///
Binds an object's properties to
<see cref="Control"/>
s with the same ID as the propery name.
///
</summary>
///
<param name="obj">
The object whose properties are being bound to forms Controls
</param>
///
<param name="container">
The control in which the form Controls reside (usually a Page or ContainerControl)
</param>
public
static
void
BindObjectToControls(
object
obj, Control container)
{
if
(obj
==
null
)
return
;
//
Get the properties of the business object
//
Type objType
=
obj.GetType();
PropertyInfo[] objPropertiesArray
=
objType.GetProperties();
foreach
(PropertyInfo objProperty
in
objPropertiesArray)
{
Control control
=
container.FindControl(objProperty.Name);
if
(control
==
null
)
continue
;
//
handle ListControls (DropDownList, CheckBoxList, RadioButtonList)
//
if
(control
is
ListControl)
{
ListControl listControl
=
(ListControl) control;
listControl.SelectedIndex
=-
1
;
string
propertyValue
=
objProperty.GetValue(obj,
null
).ToString();
ListItem listItem
=
listControl.Items.FindByValue(propertyValue);
if
(listItem
!=
null
) listItem.Selected
=
true
;
}
else
{
//
get the properties of the control
//
Type controlType
=
control.GetType();
PropertyInfo[] controlPropertiesArray
=
controlType.GetProperties();
//
test for common properties
//
bool
success
=
false
;
success
=
FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray,
"
Checked
"
,
typeof
(
bool
) );
if
(
!
success)
success
=
FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray,
"
SelectedDate
"
,
typeof
(DateTime) );
if
(
!
success)
success
=
FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray,
"
Value
"
,
typeof
(String) );
if
(
!
success)
success
=
FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray,
"
Text
"
,
typeof
(String) );
}
}
}
/**/
///
<summary>
///
Looks for a property name and type on a control and attempts to set it to the value in an object's property
///
of the same name.
///
</summary>
///
<param name="obj">
The object whose properties are being retrieved
</param>
///
<param name="objProperty">
The property of the object being retrieved
</param>
///
<param name="control">
The control whose ID matches the object's property name.
</param>
///
<param name="controlPropertiesArray">
An array of the control's properties
</param>
///
<param name="propertyName">
The name of the Control property being set
</param>
///
<param name="type">
The correct type for the Control property
</param>
///
<returns>
Boolean for whether the property was found and set
</returns>
private
static
bool
FindAndSetControlProperty(
object
obj, PropertyInfo objProperty, Control control, PropertyInfo[] controlPropertiesArray,
string
propertyName, Type type)
{
//
iterate through control properties
//
foreach
(PropertyInfo controlProperty
in
controlPropertiesArray)
{
//
check for matching name and type
//
if
(controlProperty.Name
==
propertyName
&&
controlProperty.PropertyType
==
type)
{
//
set the control's property to the business object property value
//
controlProperty.SetValue(control, Convert.ChangeType( objProperty.GetValue(obj,
null
), type) ,
null
);
return
true
;
}
}
return
false
;
}
/**/
///
<summary>
///
Binds your the values in
<see cref="Control"/>
s to a business object.
///
</summary>
///
<param name="obj">
The object whose properties are being bound to Control values
</param>
///
<param name="container">
The control in which the form Controls reside (usually a Page or ContainerControl)
</param>
public
static
void
BindControlsToObject(
object
obj, Control container)
{
if
(obj
==
null
)
return
;
//
Get the properties of the business object
//
Type objType
=
obj.GetType();
PropertyInfo[] objPropertiesArray
=
objType.GetProperties();
foreach
(PropertyInfo objProperty
in
objPropertiesArray)
{
Control control
=
container.FindControl(objProperty.Name.
ToLower());
if
(control
==
null
)
continue
;
if
(control
is
ListControl)
{
ListControl listControl
=
(ListControl) control;
if
(listControl.SelectedItem
!=
null
)
objProperty.SetValue(obj, Convert.ChangeType(listControl.SelectedItem.Value,objProperty.PropertyType),
null
);
}
else
{
//
get the properties of the control
//
Type controlType
=
control.GetType();
PropertyInfo[] controlPropertiesArray
=
controlType.GetProperties();
//
test for common properties
//
bool
success
=
false
;
success
=
FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray,
"
Text
"
,
typeof
(String) );
if
(
!
success)
success
=
FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray,
"
SelectedDate
"
,
typeof
(DateTime) );
if
(
!
success)
success
=
FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray,
"
Value
"
,
typeof
(String) );
if
(
!
success)
success
=
FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray,
"
Checked
"
,
typeof
(
bool
) );
}
}
}
/**/
///
<summary>
///
Looks for a property name and type on a control and attempts to set it to the value in an object's property
///
of the same name.
///
</summary>
///
<param name="obj">
The object whose properties are being set
</param>
///
<param name="objProperty">
The property of the object being set
</param>
///
<param name="control">
The control whose ID matches the object's property name.
</param>
///
<param name="controlPropertiesArray">
An array of the control's properties
</param>
///
<param name="propertyName">
The name of the Control property being retrieved
</param>
///
<param name="type">
The correct type for the Control property
</param>
///
<returns>
Boolean for whether the property was found and retrieved
</returns>
private
static
bool
FindAndGetControlProperty(
object
obj, PropertyInfo objProperty, Control control, PropertyInfo[] controlPropertiesArray,
string
propertyName, Type type)
{
//
iterate through control properties
//
foreach
(PropertyInfo controlProperty
in
controlPropertiesArray)
{
//
check for matching name and type
//
if
(controlProperty.Name
==
propertyName
&&
controlProperty.PropertyType
==
type)
{
//
set the control's property to the business object property value
//
try
{
objProperty.SetValue(obj, Convert.ChangeType( controlProperty.GetValue(control,
null
), type) ,
null
);
return
true
;
}
catch
{
//
the data from the form control could not be converted to objProperty.PropertyType
//
return
false
;
}
}
}
return
false
;
}
}
posted on 2005-01-05 18:57
富康
阅读(753)
评论(0)
编辑
收藏
网摘
所属分类:
系统架构
新用户注册
刷新评论列表
标题
姓名
主页
Email
(博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
网站首页
新闻频道
社区
小组
博问
网摘
人才
找找看
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
该文被作者在 2005-01-14 10:14 编辑过
Google站内搜索
China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!
开发者征途系统新作:《设计模式——基于C#的工程化实现及扩展》
相关文章:
相关链接:
所属分类的其他文章:
SOA 分类
实体类和显示控件的绑定
最新IT新闻:
Google操作系统已开始内部测试?
Google阅读器界面升级 全新改版
微软官方下载:Windows Vista SP2 Beta测试版
微软发布PC Live单机游戏客户端
Firefox遭“独家”恶意软件攻击
Powered by:
博客园
Copyright © 富康