UserData ID,Name,PWD
DropDownList SelectedIndex,SelectedValue情况1: 同一个业务对象的多个属性 能够和 同一个控件的多个属性绑定吗
DropDownList. SelectedIndex 与 UserData. ID
DropDownList. SelectedValue 与 UserData. Name情况2:同一个业务对象的同一个属性 能够和 同一个控件的多个属性绑定吗
DropDownList. SelectedIndex 与 UserData. ID
DropDownList. SelectedValue 与 UserData. ID情况3:绑定DropDownList里面,Item集合中,元素的属性
DropDownList.Items[0].Text 与 UserData. Name
DropDownList.Items[0].Value 与 UserData. ID情况4: 能与复杂的控件绑定吗,如DataList 模本中的自定义控件
请大家给一点具体的源代码,或者相关的网站联接
DropDownList SelectedIndex,SelectedValue情况1: 同一个业务对象的多个属性 能够和 同一个控件的多个属性绑定吗
DropDownList. SelectedIndex 与 UserData. ID
DropDownList. SelectedValue 与 UserData. Name情况2:同一个业务对象的同一个属性 能够和 同一个控件的多个属性绑定吗
DropDownList. SelectedIndex 与 UserData. ID
DropDownList. SelectedValue 与 UserData. ID情况3:绑定DropDownList里面,Item集合中,元素的属性
DropDownList.Items[0].Text 与 UserData. Name
DropDownList.Items[0].Value 与 UserData. ID情况4: 能与复杂的控件绑定吗,如DataList 模本中的自定义控件
请大家给一点具体的源代码,或者相关的网站联接
{
listCtrl.DataSource = iList;
listCtrl.DataTextField = textField;
listCtrl.DataValueField = valueField;
listCtrl.DataBind();
}
参数
listCtrl传入列表控件,可以是DropDownList ListBox CheclBoxList等
iList传入IList接口的实现包含业务对象的列表(集合)对象
textField 列表条目显示文本绑定的对象属性
valueField 列表条目值绑定的对象属性
{
if (obj == null) return;
Type objType = obj.GetType();
PropertyInfo[] objPropertiesArray = objType.GetProperties();
foreach (PropertyInfo objProperty in objPropertiesArray)
{
Control control = container.FindControl(objProperty.Name);
if (control != null)
{
if (control is ListControl)
{
Type controlType = control.GetType();
PropertyInfo[] controlPropertiesArray = controlType.GetProperties();
bool success = false;
success = success = FindAndSetControlProperty(obj,objProperty,control,controlPropertiesArray,"SelectedIndex",typeof(int));
if(!success) FindAndSetControlProperty(obj,objProperty,control,controlPropertiesArray,"Checked",typeof(bool));
if(!success) success = FindAndSetControlProperty(obj,objProperty,control,controlPropertiesArray,"Text",typeof(string));
if(!success) success = FindAndSetControlProperty(obj,objProperty,control,controlPropertiesArray,"SelectedDate",typeof(DateTime));
if(!success) success = FindAndSetControlProperty(obj,objProperty,control,controlPropertiesArray,"Value",typeof(string));
}
else if (control is TextBox)
{
Type controlType = control.GetType();
PropertyInfo[] controlPropertiesArray = controlType.GetProperties();
bool success = false;
success = FindAndSetControlProperty(obj,objProperty,control,controlPropertiesArray,"Text",typeof(string));
}
else if (control is LinkButton)
{
Type controlType = control.GetType();
PropertyInfo[] controlPropertiesArray = controlType.GetProperties();
bool success = false;
success = FindAndSetControlProperty(obj,objProperty,control,controlPropertiesArray,"Text",typeof(string));
if(success) success = FindAndSetControlProperty(obj,objProperty,control,controlPropertiesArray,"CommandArgument",typeof(string));
}
else if (control is Label)
{
((Label) control).Text = objProperty.GetValue(obj, null).ToString();
}
else if (control is ImageButton)
{
Type controlType = control.GetType();
PropertyInfo[] controlPropertiesArray = controlType.GetProperties();
bool success = false;
success = FindAndSetControlProperty(obj,objProperty,control,controlPropertiesArray,"ImageUrl",typeof(string));
if(!success) success = FindAndSetControlProperty(obj,objProperty,control,controlPropertiesArray,"CommandArgument",typeof(string)); }
else if (control is HyperLink)
{
Type controlType = control.GetType();
PropertyInfo[] controlPropertiesArray = controlType.GetProperties();
bool success = false;
success = FindAndSetControlProperty(obj,objProperty,control,controlPropertiesArray,"NavigateUrl",typeof(string));
if(!success) success = FindAndSetControlProperty(obj,objProperty,control,controlPropertiesArray,"ImageUrl",typeof(string));
if(!success) success = FindAndSetControlProperty(obj,objProperty,control,controlPropertiesArray,"Text",typeof(string)); }
else if (control is Image)
{
((Image) control).ImageUrl = objProperty.GetValue(obj, null).ToString();
}
else if (control is CheckBox)
{
((CheckBox) control).Checked = Convert.ToBoolean(objProperty.GetValue(obj, null));
} else
{
// 处理其他控件类型
}
} } } #endregion
#region //FindAndSetControlProperty
private static bool FindAndSetControlProperty(object obj, PropertyInfo objProperty, Control control, PropertyInfo[] controlPropertiesArray, string propertyName, Type type)
{
// 在整个控件属性中进行迭代
foreach (PropertyInfo controlProperty in controlPropertiesArray)
{
if(control is TextBox) propertyName = propertyName;
// 检查匹配的名称和类型
if (controlProperty.Name == propertyName && controlProperty.PropertyType == type)
{
// 将控件的属性设置为
// 业务对象属性值
controlProperty.SetValue(control, Convert.ChangeType( objProperty.GetValue(obj, null), type) , null);
return true;
}
}
return false;
} #endregion
ref:http://www.cnblogs.com/navicy/archive/2005/05/15/155829.html
情况2:同一个业务对象的同一个属性 能够和 同一个控件的多个属性绑定吗
情况3:绑定DropDownList里面,Item集合中,元素的属性
这三种情况毫无疑问是可以的.
第四种情况需要自己扩展了listCtrl.DataSource = iList;
// 这个属性绑定的是控件显示的值
listCtrl.DataTextField = text
// 这个属性绑定的是控件后台的值
listCtrl.DataValueField = value;
reflection
reflection
各位高人,反射能够实现上面复杂的绑定吗!
贴的代码是MSDN上下载的吧,到处都在转载这段代码,经我测试,这段代码有错误
1. 从实体到控件,必须要一个控件对应一个实体属性,少一个控件就报错,否则报错。
2. 从控件到实体,根本用不了去我的blog上看看我修正以后的代码:
http://blog.csdn.net/patrickpan
经测试,很好用。
BindObjectToControls 和 BindControlsToObject都没问题啊
用我修改以后的代码吧,微软放上去的代码和页面上的代码都有错误!