2009-09-04 4 views
5

GridView 내에서 사용되는 BoundField를 확장하는 컨트롤을 만들고 싶습니다. 내가 뭘하고 싶은 건 DataField 속성과 비슷하게 될 HighlightField라는 또 다른 속성을 제공하고 그 이름에 데이터 열 이름을 지정하려고합니다. 해당 데이터 열을 감안할 때 값이 true 또는 false인지 확인하고 지정된 행에서 주어진 열 내의 주어진 텍스트를 강조 표시합니다.(ASP.NET) BoundField 확장

즉하지 않는 경우 일부 사이비 코드는 의미 :

<asp:GridView id="grid"> 
    <Columns> 
    <asp:BoundField DataField="Name" /> 
    <cc:HighlightField DataField="Name" HighlightField="IsHighlighted" /> 
    </Columns> 
</asp:GridView> 

그리고 다음 데이터 바인딩 또는 무언가 내 :

if(this row's IsHighlighted value is true) 
    set the CssClass of this datacell = "highlighted" 
(or wrap a span tag around the text) 

황홀하게 올바른 방향으로 날을 지적, 여기에 내가이 종료 무엇 위로 :

public class HighlightedBoundField : BoundField 
{ 
    public string HighlightField 
    { 
     get { return ViewState["HighlightField"].ToString(); } 
     set 
     { 
      ViewState["HighlightField"] = value; 
      OnFieldChanged(); 
     } 
    } 

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

     bool isDataRowAndIsHighlightFieldSpecified = cellType == DataControlCellType.DataCell && !string.IsNullOrEmpty(HighlightField); 
     if (isDataRowAndIsHighlightFieldSpecified) 
     { 
      cell.DataBinding += new EventHandler(cell_DataBinding); 
     } 
    } 

    void cell_DataBinding(object sender, EventArgs e) 
    { 
     TableCell cell = (TableCell)sender; 
     object dataItem = DataBinder.GetDataItem(cell.NamingContainer); 
     cell.Text = DataBinder.GetPropertyValue(dataItem, DataField).ToString(); 

     bool highlightThisCellsText = Convert.ToBoolean(DataBinder.GetPropertyValue(dataItem, HighlightField)); 
     if (highlightThisCellsText) 
     { 
      cell.CssClass += " highlight"; 
     } 
    } 
} 

답변

5

테스트되지 않은 :

public class HighlightBoundField : DataControlField { 

    //property to indicate if this field should be highlighted, given the value of this property 
    // 
    public string HighlightField { 
     get { 
      object value = ViewState["HighlightField"]; 

      if (value != null) { 
       return Convert.ToString(value); 
      } 

      return ""; 
     } 

     set { 
      ViewState["HighlightField"] = value; 
      OnFieldChanged(); 
     } 
    } 

    //property to display as text in the cell 
    // 
    public string DataField { 
     get { 
      object value = ViewState["DataField"]; 

      if (value != null) { 
       return value.ToString(); 
      } 

      return string.Empty; 
     } 

     set { 
      ViewState["DataField"] = value; 

      OnFieldChanged(); 
     } 
    } 

    //bound field creation 
    // 
    protected override DataControlField CreateField() { 
     return new BoundField(); 
    } 

    //override the method that is used to populate and format a cell 
    // 
    public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex) { 
     base.InitializeCell(cell, cellType, rowState, rowIndex); 

     //if this celltype is a data row 
     // 
     if (cellType == DataControlCellType.DataCell && !string.IsNullOrEmpty(HighlightField)) { 
      //create label control to display text 
      // 
      var lblText = new Label(); 

      //add event listener for when the label is bound 
      // 
      lblText.DataBinding += new EventHandler(lblText_DataBinding); 

      //add label to controls collection 
      // 
      cell.Controls.Add(lblText); 
     } 
    } 

    void lblText_DataBinding(object sender, EventArgs e) { 
     //retrieve data item and set label text 
     // 
     Label lblText = (Label) sender; 
     object dataItem = DataBinder.GetDataItem(lblText.NamingContainer); 
     lblText.Text = DataBinder.GetPropertyValue(dataItem, DataField).ToString(); 

     //check if value should be highlighted 
     // 
     if (Convert.ToBoolean(DataBinder.GetPropertyValue(dataItem, HighlightField))) { 
      lblText.Style.Add("background-color", "yellow"); 
     } 
    } 
} 
+0

나는 그것을 좋아한다. 나는 그것을 소용돌이 치고 돌아올 것이다. – rball

+0

양방향 데이터 바인딩을 할 방법이없는가? "HighlightField ="Name "HighlightField = '<% # Bind ("IsHighlighted ") %>'/>"IsHighlighted "라는 HighlightBoundField에 부울 속성을 만들고 다음과 같이 할 수 있습니까? – Chris

관련 문제