2010-12-09 60 views
2

我做了一些搜索,但没有什么是真正有用的,在我的情况。自定义DataControlField类

我想继承DataControlField(System.Web.UI.WebControls),以便能够携带两个标签控件,然后我想着色两个标签以获得某种条件格式,我已经获得了条件格式化部分,但我怎样才能定制这个类?

在我的课上我应该定义两个标签控件? 我将如何重写CreateField方法? P:我知道我可以在XHTML Markup中完成这项工作,但是我有很多列,因此将这些标记包含在页面标记中并不合适。因此我在CodeBehind页面中这样做。

编辑

public class MyField : DataControlField 
{ 
    public MyField() 
    { 

    } 

    protected override DataControlField CreateField() 
    { 
     // What to put here? 
    } 

    protected override void CopyProperties(DataControlField newField) 
    { 
     ((CalendarField)newField).DataField = this.DataField; 
     ((CalendarField)newField).DataFormatString = this.DataFormatString; 
     ((CalendarField)newField).ReadOnly = this.ReadOnly; 

     base.CopyProperties(newField); 
    } 

    public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex) 
    { 
     // Call the base method 
     base.InitializeCell(cell, cellType, rowState, rowIndex); 

     // Initialize the contents of the cell quitting if it is a header/footer 
     if (cellType == DataControlCellType.DataCell) 
      InitializeDataCell(cell, rowState); 
    } 

    protected virtual void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState) 
    { 

    } 
} 
+0

后一些代码... – Saar 2010-12-09 12:52:15

+0

@Saar我提供了一些代码,但需要知道如何实现基本方法的正确途径。 – 2010-12-09 13:17:16

回答

1

在这里看到。希望这可以帮助你。

public class MyField : DataControlField {  
    public MyField()  {  }  
    protected override DataControlField CreateField()  {   
     // What to put here?  

     return new MyField(); 
    }  
    protected override void CopyProperties(DataControlField newField)  {   
     ((CalendarField)newField).DataField = this.DataField;   
     ((CalendarField)newField).DataFormatString = this.DataFormatString;   
     ((CalendarField)newField).ReadOnly = this.ReadOnly;   
     base.CopyProperties(newField);  
    }  

    public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)  
    {   
     // Call the base method   
     base.InitializeCell(cell, cellType, rowState, rowIndex);   
     // Initialize the contents of the cell quitting if it is a header/footer   
     if (cellType == DataControlCellType.DataCell) 
     { 
      cell.DataBinding += new EventHandler(cell_DataBinding); 
     } 
    } 

    void cell_DataBinding(object sender, EventArgs e) 
    { 
     Control ctrl = sender as Control; 
     var container = ctrl.NamingContainer as IDataItemContainer; 

     // here what you would like to show in MyField 
    }  

}