首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扩展(ASP.NET) BoundField

扩展(ASP.NET) BoundField
EN

Stack Overflow用户
提问于 2009-09-04 15:12:09
回答 1查看 2.1K关注 0票数 5

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

一些psuedo-code如果这没有意义的话:

代码语言:javascript
复制
<asp:GridView id="grid">
  <Columns>
    <asp:BoundField DataField="Name" />
    <cc:HighlightField DataField="Name" HighlightField="IsHighlighted" />
  </Columns>
</asp:GridView>

然后在数据绑定或其他什么东西中:

代码语言:javascript
复制
if(this row's IsHighlighted value is true)
  set the CssClass of this datacell  = "highlighted"
(or wrap a span tag around the text)

Ravish给我指明了正确的方向,以下是我最终的结论:

代码语言:javascript
复制
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";
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-09-04 16:39:57

未经测试:

代码语言:javascript
复制
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");
        }
    }
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1379871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档