开发者

Custom DataControlField Class

I did some search but nothing is really useful in my case.

I want to inherit the DataControlField (System.Web.UI.WebControls) to be able to carry two label controls and then I want to color the two labels to get some sort of conditional formatting, I've got the conditional formatting part but how can I customize this class?

Where in my class should I define the two label controls? How would I override the CreateField method?

P.S: I know I can accomplish this, in XHTML Markup, but I have so many columns that it would not be appropriate to include those markups in the page markup. Therefore I'm开发者_运维问答 doing that in the CodeBehind page.

EDIT:

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)
    {

    }
}


See here. Hope this helps you.

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
    }      

} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜