ControlManger的主要代码如下:
namespace Beyondbit.Research.Web.UI.WebControls.Common.DataBinding
{
[
ProvideProperty("DataSource",typeof(Control)),
ProvideProperty("PropertyName",typeof(Control)),
]
public class ControlManager : ControlManagerBase
{
public ControlManager() {}
#region 扩展属性
[
DefaultValue("")
]
public String GetDataSource(Control control)
{
foreach( ControlManagerSetting setting in Settings )
{
if ( control.ID == setting.Parent )
{
return setting.DataSource;
}
}
return String.Empty;
}
public void SetDataSource(Control control, String value)
{
if (value == null)
{
value = String.Empty;
}
ControlManagerSetting newSetting = null;
foreach( ControlManagerSetting setting in Settings )
{
if ( setting.Parent == control.ID )
{
newSetting = setting;
break;
}
}
if ( newSetting == null )
{
if ( value != "" )
{
newSetting = new ControlManagerSetting();
newSetting.Parent = control.ID;
newSetting.DataSource = value;
Settings.Add( newSetting );
}
}
else
{
if ( value != "" )
{
newSetting.DataSource = value;
}
else
{
Settings.Remove( newSetting );
}
}
notifyDesignerOfChange();
}
[
DefaultValue("")
]
public String GetPropertyName(Control control)
{
foreach( ControlManagerSetting setting in Settings )
{
if ( control.ID == setting.Parent )
{
return setting.PropertyName;
}
}
return String.Empty;
}
public void SetPropertyName(Control control, String value)
{
if (value == null)
{
value = String.Empty;
}
ControlManagerSetting newSetting = null;
foreach( ControlManagerSetting setting in Settings )
{
if ( setting.Parent == control.ID )
{
newSetting = setting;
break;
}
}
if ( newSetting == null )
{
if ( value != "" )
{
newSetting = new ControlManagerSetting();
newSetting.Parent = control.ID;
newSetting.PropertyName = value;
Settings.Add( newSetting );
}
}
else
{
if ( value != "" )
{
newSetting.PropertyName = value;
}
else
{
Settings.Remove( newSetting );
}
}
notifyDesignerOfChange();
}
#endregion
#region 动态绑定方法
public void UserDataBind(object obj)
{
foreach (Control control in Parent.Controls)
{
if (control is System.Web.UI.WebControls.TextBox)
{
System.Web.UI.WebControls.TextBox textBox = control as System.Web.UI.WebControls.TextBox;
foreach(ControlManagerSetting setting in Settings)
{
if(setting.Parent==control.ID)
{
Type t = obj.GetType();
PropertyInfo p = t.GetProperty(setting.PropertyName);
textBox.Text = p.GetValue(obj,null).ToString().Trim();
}
}
//下面可以分别再对其它类型的控件进行处理
if (control is System.Web.UI.WebControls.Label)
..........
}
}
}
public object BindDataRefresh(object obj)
{
foreach(Control control in Parent.Controls)
{
if(control is System.Web.UI.WebControls.TextBox)
{
System.Web.UI.WebControls.TextBox textBox = control as System.Web.UI.WebControls.TextBox;
foreach(ControlManagerSetting setting in Settings)
{
if(setting.Parent==control.ID)
{
Type t = obj.GetType();
PropertyInfo p = t.GetProperty(setting.PropertyName);
p.SetValue(obj,textBox.Text,null);
}
}
........
}
}