【IT168技术文档】
这里以自定义CalendarField列为例
看DataControlField的CloneField()方法,先创建对象,再复制属性
创建列对象protected internal DataControlField CloneField() { DataControlField newField = this.CreateField(); this.CopyProperties(newField); return newField; }
DataControlField提供了CopyProperties 方法,此工作一定要做
复制属性protected override DataControlField CreateField() { return new CalendarField(); }
DataControlField提供了CopyProperties 方法
首先要先定义你需要的属性,然后将属性复制给CloneField方法中创建的对象,在更改属性时要记得调用OnFieldChanged方法,通知DataControlField对象状态发生变化,触发FieldChanged事件
Custom properties#region Custom properties // ******************************************************************* // PROPERTY: DataField // Indicates the field providing the date in view mode public virtual string DataField { get { object o = this.ViewState["DataField"]; if (o != null) return (string) o; return String.Empty; } set { ViewState["DataField"] = value; OnFieldChanged(); } } // ******************************************************************* // PROPERTY: ReadOnly // Indicates the field from which the text of the drop-down items is taken public virtual bool ReadOnly { get { object o = base.ViewState["ReadOnly"]; if (o != null) return (bool) o; return false; } set { base.ViewState["ReadOnly"] = value; OnFieldChanged(); } } // ******************************************************************* // PROPERTY: DataFormatString // Indicates the format string for the date public virtual string DataFormatString { get { object o = this.ViewState["DataFormatString"]; if (o != null) return (string)o; return String.Empty; } set { ViewState["DataFormatString"] = value; OnFieldChanged(); } } #endregion // ******************************************************************* // METHOD: CopyProperties // protected override void CopyProperties(DataControlField newField) { ((CalendarField)newField).DataField = this.DataField; ((CalendarField)newField).DataFormatString = this.DataFormatString; ((CalendarField)newField).ReadOnly = this.ReadOnly; base.CopyProperties(newField); }