2008-11-20 27 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方法。