2009-09-29 32 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]; 
    } 

這種方法唯一的缺點是你必須調用數據綁定。我雖然如此,這對我來說並不是問題,而且工作很好。