2010-12-09 2 views
2

일부 검색을했지만 내 경우에는 아무 것도 유용하지 않습니다.Custom DataControlField 클래스

두 개의 레이블 컨트롤을 전달할 수 있도록 DataControlField (System.Web.UI.WebControls)를 상속하려면 다음 두 가지 레이블을 색으로 지정하여 일종의 조건부 서식을 지정하려면 조건부 서식이 있습니다. 형식을 지정하는 부분이지만이 클래스를 사용자 정의하려면 어떻게해야합니까?

수업 시간에 두 개의 라벨 컨트롤을 정의해야합니까? CreateField 메서드는 어떻게 재정의합니까?

P.S : XHTML 마크 업에서이 작업을 수행 할 수 있음을 알고 있지만 페이지 마크 업에 마크 업을 포함시키지 않는 것이 많은 열이 있습니다. 그러므로 나는 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

+0

@Saar 나는 몇 가지 코드를 포함하지만 기본 방법 올바른 방법을 구현하는 방법을 알아야했습니다. –

답변

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 
    }  

} 
+0

답변을 읽기 전에 새로운 MyField()를 반환해야한다는 요지를 알아 냈습니다. 고마워요. 여기서부터 계속하겠습니다. –

+1

때로는 문제가 해결되는 데 도움이되는 경우도 있습니다. :) – Saar

+0

현재 바인딩 된 필드의 데이터는 어떻게 얻을 수 있습니까? 비슷한 것 문자열 MyText = (문자열) DataItem [DataField] –