2009-09-29 68 views
0

我想根据其他因素更改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"; 
     } 
    } 

此代码为绑定列的伟大工程,该小区的文字被改变,并强调,但即使按钮控制确实有一个按钮与正确的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在cell_DataBinding方法中是string.Empty。

任何人有任何想法?

回答

0

我在数据绑定事件经过,并创建一个字符串列表发出时,预渲染被称为:

  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]; 
    } 

这种方法唯一的缺点是你必须调用数据绑定。我虽然如此,这对我来说并不是问题,而且工作很好。