どのように私は、拡張ButtonFieldの値を変更できますか?
-
18-09-2019 - |
質問
私は他の要因に応じて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とボタンを持っているんが、テキスト値は、最初は何も含まれていないとのようですどこかに設定します。私は別の値にそれをここに設定した場合、それは他の元の値のどこかに再設定しているようです。これが起こってできたところリフレクターを見ると、私は表示されません。リフレクターは示しています:
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(オブジェクト、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がcell_DataBinding方法でString.Emptyのあると思われる前に私が言ったように。
どの誰もが任意のアイデアがありますか?
解決
私は、データバインドされたイベントに通過し、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];
}
このアプローチの唯一の没落は、あなたがデータバインドを呼び出す必要があるということです。それは桃の私にとって問題ではなかったと働いていたので、私も思います。
所属していません StackOverflow