2010-06-17 111 views
1

我正在使用gridview控件在asp.net中工作。我有一個動態創建的按鈕列:Gridview更改按鈕列中的文本

  ButtonField bfSelect = new ButtonField(); 
      bfSelect.HeaderText = "View"; 
      bfSelect.ButtonType = ButtonType.Link; 
      bfSelect.CommandName = "View"; 
      bfSelect.Text = "View"; 

      grdAttachments.Columns.Add(bfSelect); 

按鈕上的文本對於每一行都是相同的。我想知道是否有任何方法讓文本根據條件不同而不同。當我嘗試查看特定行的文本屬性時,它是空白的,如果我嘗試設置它,它不會更改。

在此先感謝。

鮑勃

回答

0

有幾個方法可以做到這一點,從我的頭頂,我建議你在RowDataBind事件來改變值。

這手打字,所以可能有錯別字..

protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row .RowType == DataControlRowType .DataRow) 
    { 
    Button button = (Button)e.Row.Cells[0].Controls[0]; 
    button.Text = "Your New Value"; 
    } 
} 
1

的RowDataBound事件,您可以檢查當前行。例如:

String text = ((Label)e.Row.FindControl("aLabel")).Text; 
if(text == "Yes") 
{ 
    ((LinkButton)e.Row.FindControl("bfSelect").Text = "Good to go!"; 
} 

爲此,您還需要將按鈕的ID設置爲bfSelect。

Grz,Kris。

+0

我想這個確切的代碼,當我試圖進入。文本,該選項框不彈出。如果我無論如何寫出來,它給我的錯誤是'System.Web.UI.Control'不包含'文本'的定義。 – Jared 2012-05-04 15:20:46

0

,最好是在Javascript編輯:如果你不使用AJAX]

$("#GridView1 tr td a").html("Custom_Delete_Text"); 
$("#GridView1 tr td a").addClass("another class you wish");