2013-10-30 81 views
0

我已經在GridView LinkBut​​ton控件的文本內容:獲取LinkBut​​ton控件

enter image description here

我想在文本從這些控件得到:

protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e) 
{ 
    System.Diagnostics.Debug.WriteLine(e.Row.Cells[4].Text); 
} 

這是結果:

Task 











  

我在做什麼錯,我該如何到文本?

回答

1

您可以使用FindControl

if (e.Row.RowType == DataControlRowType.DataRow) 
{ 
    LinkButton btn= ((LinkButton)e.Row.FindControl("YourControlId")); 
    var text = btn.Text; 
    //Here you have btn object 
} 
1

您是否嘗試過類似下面?

if(e.Row.RowType == DataControlRowType.DataRow) 
{ 
    LinkButton btn = (LinkButton)e.Row.FindControl("btn"); 
} 
1

您需要跳過標題行,只處理數據行,像這樣:

protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e) 
{ 
    // Ignore all other rows types (header, footer, etc.) except data rows 
    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
     System.Diagnostics.Debug.WriteLine(e.Row.Cells[4].Text); 
    } 
} 

有關在網格視圖的數據類型行的更多信息請閱讀GridViewRow.RowType Property