如何更改扩展 ButtonField 的值?
我想根据其他因素更改 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 的按钮,文本值最初不包含任何内容,并且似乎是设置在其他地方。当我在这里将其设置为另一个值时,它似乎在其他地方重置为原始值。查看 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;
我认为在我尝试更改值之前会发生这种情况,但正如我之前所说,button.Text 似乎是 string.Empty在 cell_DataBinding 方法中。
有人有什么想法吗?
I want to change the value of the text in a ButtonField depending on other factors. Some code:
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";
}
}
This code works great for BoundField, in which the cell's Text is changed and highlighted but even though the button control does indeed have a button with the correct CommandName set through the aspx page, the Text value initially contains nothing and seems to be set somewhere else. When I set it here to another value it seems to be resetting to the original value someplace else. Looking into Reflector I don't see where this could be happening. Reflector shows:
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()
I've checked each one and I don't see the value getting set. That does seem to be happening in the OnDataBindField(object, EventArgs) here:
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;
Which I would think would be happening BEFORE I'm trying to change the value, but as I've said before it seems that button.Text is string.Empty in the cell_DataBinding method.
Anyone have any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在数据绑定事件中进行操作,并创建一个要在调用 PreRender 时发出的字符串列表:
最后
在预渲染中:
这种方法的唯一缺点是您必须调用 databind。不过我是这样的,所以这对我来说不是问题并且工作得很好。
I go through in the databound event and create a list of strings to emit when the PreRender is called:
and
and finally in the pre render:
The only downfall of this approach is that you have to call databind. I am though so it wasn't an issue for me and worked peachy.