2008-11-20 31 views
1

由于DetailsView对头文本和数据都使用<td>单元格,我想知道是否可以重写控件的行为来渲染每个行的HeaderText在<和>单元格中?有没有什么办法让DetailsView控件在<th>单元中渲染其HeaderText?


@Joel Coehoorn感谢您的快速回复,但我有点希望我不必走这条路。

我想知道是否有可能重写其中一个控件呈现方法来实现这一目标?

有人似乎有had success rendering <th> cells,但似乎没有披露细节 - 任何其他建议将受到感谢。

回答

3

我设法通过ItemCreaed事件处理和交换的<TD>细胞的<日>细胞这样找到解决的办法:

if (view.Rows.Count > 0) { 
    // swap each header <td> cell for a <th> cell 
    foreach (DetailsViewRow row in view.Rows) { 
     if (row.RowType == DataControlRowType.DataRow) { 
      DataControlFieldCell td = row.Cells[0] as DataControlFieldCell; 
      // skip the last row that contains our command controls 
      if (td.Controls.Count > 0) { 
       continue; 
      } 

      DataControlFieldHeaderCell th = new DataControlFieldHeaderCell(td.ContainingField); 
      th.Text = td.Text; 
      th.Attributes.Add("scope", "row"); 

      // add the new th and remove the old td 
      row.Cells.RemoveAt(0); 
      row.Cells.AddAt(0, th); 
     } 
    } 
} 
1

控制本身没有内置选项。但是,您可以使用Control Adapter完全覆盖任何控件的渲染行为,包括DetailsView。

1

你也可以继承自己的自定义控制DetailsView,然后重写render方法。