2010-12-09 37 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 
    }  

}