日期:2014-05-18  浏览次数:20811 次

如何取得动态绑定的模板列的值?
我的数据结构如下:
Calendar MonthHours projectNo subTeam1(Hours) subTeam2(Hours) subTeam3(Hours) .....subTeamn(Hours)
-------- -------- ----------- ------------ ----------- -----------
2012-05 160 6666 30 50 40 ...... 20
2012-06 176 6666 40 30 30 ...... 50
2012-07 168 6666 20 40 20 ...... 40
.......

List.Columns.Clear();

  BoundField wpnobf = new BoundField();
  wpnobf.DataField = "WPMonth";
  wpnobf.HeaderText = "Calendar";
  List.Columns.Add(wpnobf);

  BoundField companyhoursbf = new BoundField();
  companyhoursbf.DataField = "currentmonthhours";
  companyhoursbf.HeaderText = "MonthHours";
  List.Columns.Add(companyhoursbf);

  dt = project.GetSubTeamName(wpNo);
  foreach (DataRow dr in dt.Rows)
  {
  TemplateField subteamField = new TemplateField();
  subteamField.ShowHeader = true;
   
  subteamField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, dr[0].ToString(), dr[0].ToString());//添加的列标题 

  GridViewTemplate gvt = new GridViewTemplate(DataControlRowType.DataRow, dr[0].ToString(), dr[0].ToString());
  subteamField.ItemTemplate = gvt;

  List.Columns.Add(subteamField);
  }

public class GridViewTemplate : ITemplate  
{
public delegate void EventHandler(object sender, EventArgs e);  
  public event EventHandler eh;  
  private DataControlRowType templateType;  
  private string columnName;  
  private string controlID;  

  public GridViewTemplate(DataControlRowType type, string colname)  
  {  
  templateType = type;  
  columnName = colname;  
  }  

  public GridViewTemplate(DataControlRowType type, string controlID, string colname)  
  {  
  templateType = type;  
  this.controlID = controlID;  
  columnName = colname;  
  } 
 
  public void InstantiateIn(System.Web.UI.Control container)  
  {  
  switch (templateType)  
  {  
  case DataControlRowType.Header:  
  Literal lc = new Literal();  
  lc.Text = columnName;
  lc.ID = controlID;
  container.Controls.Add(lc);  
  break;  

  case DataControlRowType.DataRow://可以定义自己想显示的控件以及绑定事件 
  TextBox tb = new TextBox();
  tb.ID = columnName;
  tb.ID = controlID;
  container.Controls.Add(tb);