如何更改扩展 ButtonField 的值?

发布于 2024-08-06 08:10:40 字数 2452 浏览 9 评论 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,其中单元格的文本被更改并突出显示,但即​​使按钮控件确实有一个通过 aspx 页面设置了正确的 CommandName 的按钮,文本值最初不包含任何内容,并且似乎是设置在其他地方。当我在这里将其设置为另一个值时,它似乎在其他地方重置为原始值。查看 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;

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

有人有什么想法吗?

I want to change the value of the text in a ButtonField depending on other factors. Some code:

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

This code works great for BoundField, in which the cell's Text is changed and highlighted but even though the button control does indeed have a button with the correct CommandName set through the aspx page, the Text value initially contains nothing and seems to be set somewhere else. When I set it here to another value it seems to be resetting to the original value someplace else. Looking into Reflector I don't see where this could be happening. Reflector shows:

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

I've checked each one and I don't see the value getting set. That does seem to be happening in the OnDataBindField(object, EventArgs) here:

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;

Which I would think would be happening BEFORE I'm trying to change the value, but as I've said before it seems that button.Text is string.Empty in the cell_DataBinding method.

Anyone have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三寸金莲 2024-08-13 08:10:40

我在数据绑定事件中进行操作,并创建一个要在调用 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。不过我是这样的,所以这对我来说不是问题并且工作得很好。

I go through in the databound event and create a list of strings to emit when the PreRender is called:

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

and

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

and finally in the pre render:

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

The only downfall of this approach is that you have to call databind. I am though so it wasn't an issue for me and worked peachy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文