skip to main |
skip to sidebar
RSS Feeds
ASP.NET, JavaScript, Oracle and SQL articles and code.
ASP.NET, JavaScript, Oracle and SQL articles and code.
3:58 AM
Posted by Michael Heliso
Customer: <%# custID %>Where "Customer" is a label and "custID" is a public property.
namespace MyControls { public enum DataTypes { Default, String, Integer, DateTime, Double } }
namespace MyControls { public interface IDataBound { DataTypes DataType { get; set; } string BoundColumn { get; set; } object BoundValue { get; set; } bool SingleBind { get; set; } } }
namespace MyControls { public interface IDataBoundInfo : IDataBound { string TableName { get; set; } } }
using System; using System.Web.UI.WebControls; /// <summary> /// Summary description for BindingTextBox /// </summary> namespace MyControls { public class BindingTextBox : TextBox, IDataBoundInfo { /// <summary> /// IDataBound members. /// </summary> private DataTypes _datatype = DataTypes.Default; private string _boundcolumn; private bool _singlebind; /// <summary> /// IDataBoundInfo members. /// </summary> private string _tablename; public DataTypes DataType { get { return _datatype; } set { _datatype = value; } } public string BoundColumn { get { return _boundcolumn; } set { _boundcolumn = value; } } public virtual object BoundValue { get { return ControlHelper.ConvertValue (_datatype, this.Text); } set { if (value is DBNull) this.Text = ""; else this.Text = value.ToString(); } } public bool SingleBind { get { return _singlebind; } set { _singlebind = value; } } public string TableName { get { return _tablename; } set { _tablename = value; } } } }
using System; /// <summary> /// Summary description for ControlHelper /// </summary> namespace MyControls { public class ControlHelper { public static object ConvertValue(DataTypes toType, object value) { try { switch (toType) { case DataTypes.String: return Convert.ToString(value); case DataTypes.Integer: return Convert.ToInt32(value); case DataTypes.DateTime: return Convert.ToDateTime(value); case DataTypes.Double: return Convert.ToDouble(value); case DataTypes.Default: return value; } } catch { return null; } return null; } } }
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Web.UI; using System.Web.UI.WebControls; /// <summary> /// Summary description for BindingPanel /// </summary> namespace MyControls { public class BindingPanel : Panel { private string _data_member; private object _datasource; #region public string DataMember [Browsable(false)] public string DataMember { get { return _data_member; } set { _data_member = value; } } #endregion #region public object DataSource [Browsable(false)] public object DataSource { get { return _datasource; } set { if ((value == null) || (value is IListSource) || (value is IEnumerable)) { _datasource = value; } else throw new ArgumentException(@"Invalid object. Object must implement IListSource or IEnumerable", "DataSource"); } } #endregion #region private void UpdateFromControlsRecursive (Control control, object row) private void updateFromControlsRecursive (Control control, object row) { foreach (Control ctrl in control.Controls) { if (ctrl is IDataBound) { IDataBound idbc = (IDataBound)ctrl; string boundField = idbc.BoundColumn; object _old_value = null; if (boundField.Length > 0) { if (row is DataRow) _old_value = ((DataRow)row)[boundField]; if (_old_value != idbc.BoundValue) { if (row is DataRow) { if (idbc.BoundValue != null) ((DataRow)row)[boundField] = idbc.BoundValue; else ((DataRow)row)[boundField] = DBNull.Value; } } } } } } #endregion #region private void BindControlsRecursive(Control control, object row) private void bindControlsRecursive(Control control, object row) { foreach (Control ctrl in control.Controls) { if (ctrl is IDataBound) { IDataBound idbc = (IDataBound)ctrl; string boundField = idbc.BoundColumn; if (boundField != null && boundField.Length > 0) { if (row is DataRow) idbc.BoundValue = ((DataRow)row)[boundField]; } } } } #endregion #region private void clearControlsRecursive(Control control) private void clearControlsRecursive(Control control) { foreach (Control ctrl in control.Controls) { if (ctrl is IDataBound) { IDataBound idbc = (IDataBound)ctrl; string boundField = idbc.BoundColumn; if (boundField != null && boundField.Length > 0) idbc.BoundValue = DBNull.Value; } } } #endregion #region private PropertyDescriptor[] GetColumnPropertyDescriptors(object dataItem) private PropertyDescriptor[] GetColumnPropertyDescriptors(object dataItem) { ArrayList props = new ArrayList(); PropertyDescriptorCollection propDescps = TypeDescriptor.GetProperties(dataItem); foreach (PropertyDescriptor pd in propDescps) { Type propType = pd.PropertyType; TypeConverter converter = TypeDescriptor.GetConverter(propType); if ((converter != null) && converter.CanConvertTo(typeof(string))) props.Add(pd); } props.Sort(new PropertyDescriptorComparer()); PropertyDescriptor[] columns = new PropertyDescriptor[props.Count]; props.CopyTo(columns, 0); return columns; } #endregion #region protected virtual IEnumerable GetDataSource() protected virtual IEnumerable GetDataSource() { if (_datasource == null) return null; IEnumerable resolvedDataSource = _datasource as IEnumerable; if (resolvedDataSource != null) return resolvedDataSource; IListSource listDataSource = _datasource as IListSource; if (listDataSource != null) { IList listMember = listDataSource.GetList(); if (listDataSource.ContainsListCollection == false) return (IEnumerable)listMember; ITypedList typedListMember = listMember as ITypedList; if (typedListMember != null) { PropertyDescriptorCollection propDescps = typedListMember.GetItemProperties(null); PropertyDescriptor propertyMember = null; if ((propDescps != null) && (propDescps.Count != 0)) { string dataMember = DataMember; if (dataMember != null) { if (dataMember.Length == 0) propertyMember = propDescps[0]; else propertyMember = propDescps.Find(dataMember, true); if (propertyMember != null) { object listRow = listMember[0]; object list = propertyMember.GetValue(listRow); if (list is IEnumerable) return (IEnumerable)list; } } throw new Exception("A list that coresponds to the selected DataMember cannot be found."); } throw new Exception("The DataSource does not contain any data members to bind to."); } } return null; } #endregion #region public void BindControls(DataRow row) public void BindControls(DataRow row) { bindControlsRecursive(this, row); } #endregion #region public void BindControls(object datasource) public void BindControls(object datasource) { bindControlsRecursive(this, datasource); } #endregion #region public void ClearControls() public void ClearControls() { clearControlsRecursive(this); } #endregion #region public void UpdateFromControls(DataRow row) public void UpdateFromControls(DataRow row) { updateFromControlsRecursive(this, row); } #endregion #region public void UpdateFromControls(object datasource) public void UpdateFromControls(object datasource) { updateFromControlsRecursive(this, datasource); } #endregion #region public override void DataBind() public override void DataBind() { IEnumerable dataSource = null; base.OnDataBinding(EventArgs.Empty); dataSource = GetDataSource(); if (dataSource != null) { PropertyDescriptor[] properties = null; foreach (Control ctrl in this.Controls) { if (ctrl is IDataBound) { IDataBound idbc = (IDataBound)ctrl; string boundField = idbc.BoundColumn; if (boundField.Length > 0) { foreach (object dataItem in dataSource) { properties = GetColumnPropertyDescriptors(dataItem); for (int i = 0; i < properties.Length; i++) { PropertyDescriptor pd = properties[i]; if (boundField.CompareTo(pd.Name) == 0) { object ctlValue = pd.GetValue(dataItem); idbc.BoundValue = pd.Converter.ConvertTo(ctlValue, typeof(string)); } } if (idbc.SingleBind) break; } } } } } } #endregion #region NESTED CLASSES #region private sealed class PropertyDescriptorComparer : IComparer private sealed class PropertyDescriptorComparer : IComparer { public int Compare(object objectA, object objectB) { PropertyDescriptor pd1 = (PropertyDescriptor)objectA; PropertyDescriptor pd2 = (PropertyDescriptor)objectB; return String.Compare(pd1.Name, pd2.Name); } } #endregion #endregion } }
if (_datasource == null) return null; IEnumerable resolvedDataSource = _datasource as IEnumerable; if (resolvedDataSource != null) return resolvedDataSource; IListSource listDataSource = _datasource as IListSource; if (listDataSource != null) {.....}
IList listMember = listDataSource.GetList(); if (listDataSource.ContainsListCollection == false) return (IEnumerable)listMember;
ITypedList typedListMember = listMember as ITypedList; if (typedListMember != null) { PropertyDescriptorCollection propDescps = typedListMember.GetItemProperties(null) if ((propDescps != null) && (propDescps.Count != 0)) { string dataMember = DataMember; if (dataMember != null) { if (dataMember.Length == 0) propertyMember = propDescps[0]; else propertyMember = propDescps.Find(dataMember, true); if (propertyMember != null) { object listRow = listMember[0]; object list = propertyMember.GetValue(listRow); if (list is IEnumerable) return (IEnumerable)list; } } throw new Exception("A list that coresponds to the selected DataMember can not be found."); } throw new Exception("The DataSource does not contains any data members to bind to."); }
private PropertyDescriptor[] GetColumnPropertyDescriptors(object dataItem) { ArrayList props = new ArrayList(); PropertyDescriptorCollection propDescps = TypeDescriptor.GetProperties(dataItem); foreach (PropertyDescriptor pd in propDescps) { Type propType = pd.PropertyType; TypeConverter converter = TypeDescriptor.GetConverter(propType); if ((converter != null) && converter.CanConvertTo(typeof(string))) props.Add(pd); } props.Sort(new PropertyDescriptorComparer()); PropertyDescriptor[] columns = new PropertyDescriptor[props.Count]; props.CopyTo(columns, 0); return columns; }
public override void DataBind() { IEnumerable dataSource = null; base.OnDataBinding(EventArgs.Empty); dataSource = GetDataSource(); if (dataSource != null) { PropertyDescriptor[] properties = null; foreach (Control ctrl in this.Controls) { if (ctrl is IDataBound) { IDataBound idbc = (IDataBound)ctrl; string boundField = idbc.BoundColumn; if (boundField.Length > 0) { foreach (object dataItem in dataSource) { properties = GetColumnPropertyDescriptors(dataItem); for (int i = 0; i < properties.Length; i++) { PropertyDescriptor pd = properties[i]; if (boundField.CompareTo(pd.Name) == 0) { object ctlValue = pd.GetValue(dataItem); idbc.BoundValue = pd.Converter.ConvertTo(ctlValue, typeof(string)); } } if (idbc.SingleBind) break; } } } } } }
dataSource = GetDataSource()
foreach (Control ctrl in this.Controls)
if (ctrl is IDataBound) { IDataBound idbc = (IDataBound)ctrl; string boundField = idbc.BoundColumn; }
foreach (object dataItem in dataSource) { properties = GetColumnPropertyDescriptors(dataItem); }
for (int i = 0; i < properties.Length; i++) { PropertyDescriptor pd = properties[i]; if (boundField.CompareTo(pd.Name) == 0) { object ctlValue = pd.GetValue(dataItem); idbc.BoundValue = pd.Converter.ConvertTo(ctlValue, typeof(string)); } }
private void bindControlsRecursive(Control control, object row) { foreach (Control ctrl in control.Controls) { if (ctrl is IDataBound) { IDataBound idbc = (IDataBound)ctrl; string boundField = idbc.BoundColumn; if (boundField != null && boundField.Length > 0) { if (row is DataRow) idbc.BoundValue = ((DataRow)row)[boundField]; } } } }
private void updateFromControlsRecursive(Control control, object row) { foreach (Control ctrl in control.Controls) { if (ctrl is IDataBound) { IDataBound idbc = (IDataBound)ctrl; string boundField = idbc.BoundColumn; object _old_value = null; if (boundField.Length > 0) { if (row is DataRow) _old_value = ((DataRow)row)[boundField]; if (_old_value != idbc.BoundValue) { if (row is DataRow) { if (idbc.BoundValue != null) ((DataRow)row)[boundField] = idbc.BoundValue; else ((DataRow)row)[boundField] = DBNull.Value; } } } } } }
private void clearControlsRecursive(Control control) { foreach (Control ctrl in control.Controls) { if (ctrl is IDataBound) { IDataBound idbc = (IDataBound)ctrl; string boundField = idbc.BoundColumn; if (boundField != null && boundField.Length > 0) idbc.BoundValue = DBNull.Value; } } }