我想根据其他因素更改 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。
有人有想法么?