实体类和显示控件的绑定

ExpandedBlockStart.gif ContractedBlock.gif      public   class  FormBinding  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Binds an object's properties to <see cref="Control"/>s with the same ID as the propery name. 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="obj">The object whose properties are being bound to forms Controls</param>
ExpandedSubBlockEnd.gif        
/// <param name="container">The control in which the form Controls reside (usually a Page or ContainerControl)</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static void BindObjectToControls(object obj, Control container) dot.gif{
InBlock.gif            
if (obj == nullreturn;
InBlock.gif            
InBlock.gif            
// Get the properties of the business object
InBlock.gif            
//
InBlock.gif
            Type objType = obj.GetType();
InBlock.gif            PropertyInfo[] objPropertiesArray 
= objType.GetProperties();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
foreach (PropertyInfo objProperty in objPropertiesArray) dot.gif{
InBlock.gif
InBlock.gif                Control control 
= container.FindControl(objProperty.Name);
InBlock.gif                
if (control==null)continue;
InBlock.gif                
// handle ListControls (DropDownList, CheckBoxList, RadioButtonList)
InBlock.gif                
//
ExpandedSubBlockStart.gifContractedSubBlock.gif
                if (control is ListControl) dot.gif{
InBlock.gif                    ListControl listControl 
= (ListControl) control;
InBlock.gif                    listControl.SelectedIndex
=-1;
InBlock.gif                    
string propertyValue = objProperty.GetValue(obj, null).ToString();
InBlock.gif                    ListItem listItem 
= listControl.Items.FindByValue(propertyValue);
InBlock.gif                    
if (listItem != null) listItem.Selected = true;
InBlock.gif                
ExpandedSubBlockStart.gifContractedSubBlock.gif                }
 else dot.gif{
InBlock.gif                    
// get the properties of the control
InBlock.gif                    
//
InBlock.gif
                    Type controlType = control.GetType();
InBlock.gif                    PropertyInfo[] controlPropertiesArray 
= controlType.GetProperties();
InBlock.gif
InBlock.gif                    
// test for common properties
InBlock.gif                    
//
InBlock.gif
                    bool success = false;
InBlock.gif                    success 
= FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Checked"typeof(bool) );
InBlock.gif                        
InBlock.gif                    
if (!success) 
InBlock.gif                        success 
= FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "SelectedDate"typeof(DateTime) );
InBlock.gif
InBlock.gif                    
if (!success) 
InBlock.gif                        success 
= FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Value"typeof(String) );
InBlock.gif                        
InBlock.gif                    
if (!success) 
InBlock.gif                        success 
= FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Text"typeof(String) );
InBlock.gif                        
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Looks for a property name and type on a control and attempts to set it to the value in an object's property 
InBlock.gif        
/// of the same name.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="obj">The object whose properties are being retrieved</param>
InBlock.gif        
/// <param name="objProperty">The property of the object being retrieved</param>
InBlock.gif        
/// <param name="control">The control whose ID matches the object's property name.</param>
InBlock.gif        
/// <param name="controlPropertiesArray">An array of the control's properties</param>
InBlock.gif        
/// <param name="propertyName">The name of the Control property being set</param>
InBlock.gif        
/// <param name="type">The correct type for the Control property</param>
ExpandedSubBlockEnd.gif        
/// <returns>Boolean for whether the property was found and set</returns>

ExpandedSubBlockStart.gifContractedSubBlock.gif        private static bool FindAndSetControlProperty(object obj, PropertyInfo objProperty, Control control, PropertyInfo[] controlPropertiesArray, string propertyName, Type type) dot.gif{
InBlock.gif            
// iterate through control properties
InBlock.gif            
//
ExpandedSubBlockStart.gifContractedSubBlock.gif
            foreach (PropertyInfo controlProperty in controlPropertiesArray) dot.gif{
InBlock.gif                
// check for matching name and type
InBlock.gif                
//
ExpandedSubBlockStart.gifContractedSubBlock.gif
                if (controlProperty.Name == propertyName && controlProperty.PropertyType == type) dot.gif{
InBlock.gif                    
// set the control's property to the business object property value
InBlock.gif                    
//
InBlock.gif
                    controlProperty.SetValue(control, Convert.ChangeType( objProperty.GetValue(obj, null), type) , null);
InBlock.gif                    
return true;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Binds your the values in <see cref="Control"/>s to a business object.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="obj">The object whose properties are being bound to Control values</param>
ExpandedSubBlockEnd.gif        
/// <param name="container">The control in which the form Controls reside (usually a Page or ContainerControl)</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static void BindControlsToObject(object obj, Control container) dot.gif{
InBlock.gif            
if (obj == nullreturn;
InBlock.gif            
InBlock.gif            
// Get the properties of the business object
InBlock.gif            
//            
InBlock.gif
            Type objType = obj.GetType();
InBlock.gif            PropertyInfo[] objPropertiesArray 
= objType.GetProperties();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
foreach (PropertyInfo objProperty in objPropertiesArray) dot.gif{
InBlock.gif
InBlock.gif                Control control 
= container.FindControl(objProperty.Name.ToLower());
InBlock.gif                
if (control==null)continue;
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (control is ListControl) dot.gif{
InBlock.gif                    ListControl listControl 
= (ListControl) control;
InBlock.gif                    
if (listControl.SelectedItem != null)
InBlock.gif                        objProperty.SetValue(obj, Convert.ChangeType(listControl.SelectedItem.Value,objProperty.PropertyType), 
null);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                }
 else dot.gif{
InBlock.gif                    
// get the properties of the control
InBlock.gif                    
//
InBlock.gif
                    Type controlType = control.GetType();
InBlock.gif                    PropertyInfo[] controlPropertiesArray 
= controlType.GetProperties();
InBlock.gif
InBlock.gif                    
// test for common properties
InBlock.gif                    
//
InBlock.gif
                    bool success = false;
InBlock.gif                    success 
= FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Text"typeof(String) );
InBlock.gif
InBlock.gif                    
if (!success) 
InBlock.gif                        success 
= FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "SelectedDate"typeof(DateTime) );
InBlock.gif
InBlock.gif                    
if (!success) 
InBlock.gif                        success 
= FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Value"typeof(String) );
InBlock.gif
InBlock.gif                    
if (!success) 
InBlock.gif                        success 
= FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Checked"typeof(bool) );
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Looks for a property name and type on a control and attempts to set it to the value in an object's property 
InBlock.gif        
/// of the same name.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="obj">The object whose properties are being set</param>
InBlock.gif        
/// <param name="objProperty">The property of the object being set</param>
InBlock.gif        
/// <param name="control">The control whose ID matches the object's property name.</param>
InBlock.gif        
/// <param name="controlPropertiesArray">An array of the control's properties</param>
InBlock.gif        
/// <param name="propertyName">The name of the Control property being retrieved</param>
InBlock.gif        
/// <param name="type">The correct type for the Control property</param>
ExpandedSubBlockEnd.gif        
/// <returns>Boolean for whether the property was found and retrieved</returns>

ExpandedSubBlockStart.gifContractedSubBlock.gif        private static bool FindAndGetControlProperty(object obj, PropertyInfo objProperty, Control control, PropertyInfo[] controlPropertiesArray, string propertyName, Type type) dot.gif{
InBlock.gif            
// iterate through control properties
InBlock.gif            
//
ExpandedSubBlockStart.gifContractedSubBlock.gif
            foreach (PropertyInfo controlProperty in controlPropertiesArray) dot.gif{
InBlock.gif                
// check for matching name and type
InBlock.gif                
//
ExpandedSubBlockStart.gifContractedSubBlock.gif
                if (controlProperty.Name == propertyName && controlProperty.PropertyType == type) dot.gif{
InBlock.gif                    
// set the control's property to the business object property value
InBlock.gif                    
//
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    try dot.gif{
InBlock.gif                        objProperty.SetValue(obj, Convert.ChangeType( controlProperty.GetValue(control, 
null), type) , null);
InBlock.gif                        
return true;
ExpandedSubBlockStart.gifContractedSubBlock.gif                    }
 catch dot.gif{
InBlock.gif                        
// the data from the form control could not be converted to objProperty.PropertyType
InBlock.gif                        
//
InBlock.gif
                        return false;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/fanjunhan/archive/2005/01/05/86909.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值