我想根据其他因素更改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值最初也不包含任何内容,并且似乎是设置在其他地方。当我将它设置为另一个值时,它似乎在其他地方重置为原始值。调查反射器我不知道这可能发生在哪里。反射器显示:
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.Temp似乎是string.Empty。
有人有什么想法吗?
答案 0 :(得分: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];
}
这种方法的唯一缺点是你必须调用数据绑定。我虽然如此,这对我来说并不是一个问题,并且很好地工作。