赏梅斋

关注微软技术

博客园 首页 新随笔 联系 订阅 管理

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);
      }
     }
    ........
    }
}

posted on 2004-08-17 14:10  赏梅斋  阅读(1968)  评论(18编辑  收藏  举报