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が設定されたボタンが実際にありますが、Text値には最初は何も含まれておらず、別の場所に設定されているようです。 。ここで別の値に設定すると、別の場所で元の値にリセットされているようです。リフレクターを調べてみると、これがどこで起こっているのかわかりません。リフレクターショー:

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メソッドでは空です。

誰かアイデアはありますか?

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 に答える