2009-09-04 9 views
5

मैं एक ऐसा नियंत्रण बनाना चाहता हूं जो ग्रिडव्यू के भीतर उपयोग किए जाने वाले बाउंडफिल्ड को बढ़ाता है। मैं जो करना चाहता हूं वह हाइलाइटफिल्ड नाम की एक और संपत्ति प्रदान करता है जो डेटाफ़ील्ड संपत्ति के समान होगा, जिसमें मैं इसे डेटा कॉलम नाम देना चाहता हूं। यह देखते हुए कि डेटा कॉलम यह देखेगा कि मान सही है या गलत है और दी गई पंक्ति पर दिए गए कॉलम के भीतर दिए गए टेक्स्ट को हाइलाइट करें।एक विस्तार (एएसपी.नेट) बाउंडफ़िल्ल्ड

कुछ छद्म-कोड है कि अगर नहीं है भावना:

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

और फिर DataBind या कुछ और के भीतर:

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

untested है:

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

दो-तरफा डाटाबेसिंग करने का कोई तरीका नहीं है? क्या आप "हाइलाइटलाइट" नामक अपने हाइलाइटबाउंडफिल्ड पर बूलियन प्रॉपर्टी बना सकते हैं और ऐसा कुछ कर सकते हैं: Chris

संबंधित मुद्दे