【问题标题】:Extending a (ASP.NET) BoundField扩展 (ASP.NET) BoundField
【发布时间】:2010-11-25 16:15:50
【问题描述】:

我想创建一个控件来扩展 GridView 中使用的 BoundField。我想做的是提供另一个名为 HighlightField 的属性,该属性类似于 DataField 属性,因为我想给它一个数据列的名称。给定该数据列,它将查看该值是真还是假,并在给定行的给定列中突出显示给定文本。

一些伪代码,如果这没有意义:

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

Ravish 为我指明了正确的方向,这就是我最终得到的结果:

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";
        }
    }
}

【问题讨论】:

    标签: asp.net databound-controls


    【解决方案1】:

    未经测试:

    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");
            }
        }
    }
    

    【讨论】:

    • 我喜欢它,我会试一试然后回来
    • 有没有办法做双向数据绑定?您能否在 HighlightBoundField 上创建一个名为“IsHighlighted”的布尔属性并执行以下操作:
    猜你喜欢
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多