0

我想根据其他因素更改 ButtonField 中文本的值。一些代码:

    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(UnnpiFlagField);
        if (isDataRowAndIsHighlightFieldSpecified)
        {
            cell.DataBinding += new EventHandler(cell_DataBinding);
        }
    }

    private void cell_DataBinding(object sender, EventArgs e)
    {
        TableCell cell = (TableCell)sender;
        object dataItem = DataBinder.GetDataItem(cell.NamingContainer);

        IButtonControl button = cell.Controls[0] as IButtonControl;
        button.Text = DataBinder.GetPropertyValue(dataItem, DataTextField).ToString();
        bool highlightTheText = DataBinder.GetPropertyValue(dataItem, IsFlagField).ToString().ToUpper() == "Y";
        if (highlightTheText)
        {
            cell.CssClass = string.Concat(ItemStyle.CssClass, " thisChangesFine");
            button.Text = "CHANGE ME";
        }
    }

此代码适用于 BoundField,其中单元格的 Text 已更改并突出显示,但即​​使按钮控件确实有一个通过 aspx 页面设置的具有正确 CommandName 的按钮,Text 值最初不包含任何内容,并且似乎设置在其他位置. 当我在这里将其设置为另一个值时,它似乎正在其他地方重置为原始值。看着 Reflector,我看不出这可能发生在哪里。反光板显示:

protected override DataControlField CreateField()
public override bool Initialize(bool sortingEnabled, Control control)
private void OnDataBindField(object sender, EventArgs e) [Can't get that one :(]
protected override void CopyProperties(DataControlField newField)
protected virtual string FormatDataTextValue(object dataTextValue)
public override void ValidateSupportsCallback()

我已经检查了每一个,但没有看到设置的值。这似乎发生在 OnDataBindField(object, EventArgs) 这里:

if ((this.textFieldDesc == null) && (component != null))
    {
        ...
        this.textFieldDesc = TypeDescriptor.GetProperties(component).Find(dataTextField, true);
        ...
    } 
    if ((this.textFieldDesc != null) && (component != null))
    {
        object dataTextValue = this.textFieldDesc.GetValue(component);
        str = this.FormatDataTextValue(dataTextValue);
    }
  ...
  ((IButtonControl) control).Text = str;

在我尝试更改值之前,我认为会发生这种情况,但正如我之前所说,在 cell_DataBinding 方法中 button.Text 似乎是 string.Empty。

有人有想法么?

4

1 回答 1

0

我通过数据绑定事件并创建一个字符串列表,以便在调用 PreRender 时发出:

            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(SpecialField);
        if (isDataRowAndIsHighlightFieldSpecified)
        {
            cell.DataBinding += new EventHandler(cell_DataBinding);
            cell.PreRender += new EventHandler(cell_PreRender);
        }
    }

    IList<string> buttonsText = new List<string>();
    private void cell_DataBinding(object sender, EventArgs e)
    {
        TableCell cell = (TableCell)sender;
        object dataItem = DataBinder.GetDataItem(cell.NamingContainer);
        bool specialData = DataBinder.GetPropertyValue(dataItem, SpecialField);
        if (specialData)
        {
            cell.CssClass = string.Concat(ItemStyle.CssClass, " special");
            buttonsText.Add("Replace text");
        }
        else
        {
            buttonsText.Add(DataBinder.GetPropertyValue(dataItem, DataTextField).ToString());
        }
    }

最后在预渲染中:

    void cell_PreRender(object sender, EventArgs e)
    {
        TableCell cell = (TableCell)sender;
        IButtonControl button = cell.Controls[0] as IButtonControl;
        int index = ((GridViewRow)cell.NamingContainer).RowIndex;
        button.Text = buttonsText[index];
    }

这种方法的唯一缺点是您必须调用 databind。不过我是,所以这对我来说不是问题,而且工作得很好。

于 2009-10-20T16:17:27.037 回答