如何更改扩展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";
}
}
此代码绑定列的伟大工程,该小区的文字被改变,并强调,但即使按钮控制确实有正确的CommandName通过aspx页面设置按钮,文本价值进行初始包含什么,似乎是设置在其他地方。当我在这里将它设置为另一个值似乎被重置为初始值别的地方。展望反射我看不出这可能发生。反射所示:
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是的String.Empty在cell_DataBinding方法。
任何人有什么想法?
解决方案
我通过在数据绑定事件并创建字符串的列表以发射时的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