2014年8月7日 星期四

ASP.NET 取得GridView Cells下所有Label物件

ASP.NET 取得GridView Cells下所有Label物件

C#:
  1. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  2. {
  3. try
  4. {
  5. if (e.Row.RowType != DataControlRowType.DataRow) // 非資料行, 離開
  6. return;
  7.  
  8. //單筆, 取得指定名稱Label物件, 若Text="n/a", 將Text改為空白
  9. Label lblGvptz_zoom = e.Row.FindControl("lblGvptz_zoom") as Label;
  10. if (lblGvptz_zoom.Text.Equals("n/a"))
  11. lblGvptz_zoom.Text = "";
  12.  
  13. //多筆迴圈, 取得Row下所以Cells中的Label物件, 若Text="n/a", 將Text改為空白
  14. foreach (TableCell cl in e.Row.Cells)
  15. {
  16. foreach (object ctrl in cl.Controls)
  17. {
  18. if (ctrl is System.Web.UI.WebControls.Label) //判斷ctrl是否為Label物件
  19. {
  20. Label lblctrl = (Label)ctrl;
  21. if (lblctrl.Text.Equals("n/a"))
  22. lblctrl.Text = "";
  23. }
  24. }
  25. }
  26. }
  27. catch (Exception)
  28. {
  29. throw;
  30. }
  31. }

沒有留言:

張貼留言