加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

扩展(ASP.NET)BoundField

发布时间:2020-12-16 06:56:28 所属栏目:asp.Net 来源:网络整理
导读:我想创建一个控件来扩展在GridView中使用的BoundField.我想要做的是提供另一个名为HighlightField的属性,它与DataField属性类似,我想给它命名一个数据列.给定数据列,它将看到值是true还是false,并在给定行的给定列中突出显示给定文本. 一些伪代码如果没有意
我想创建一个控件来扩展在GridView中使用的BoundField.我想要做的是提供另一个名为HighlightField的属性,它与DataField属性类似,我想给它命名一个数据列.给定数据列,它将看到值是true还是false,并在给定行的给定列中突出显示给定文本.

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

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

解决方法

未经测试:

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,int rowIndex) {
        base.InitializeCell(cell,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");
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读