ASP.NET 2.0 - 带有tbody / thead的DataGrid

时间:2009-02-18 17:13:26

标签: asp.net datagrid

有没有办法让DataGrid控件呈现tbody和thead HTML元素?

4 个答案:

答案 0 :(得分:9)

虽然我喜欢“user186197”的答案,但该博客帖子使用反射,但在非完全信任的托管环境中可能会出错。这是我们使用的,没有黑客:

public class THeadDataGrid : System.Web.UI.WebControls.DataGrid
{
    protected override void OnPreRender(EventArgs e)
    {
        this.UseAccessibleHeader = true; //to make sure we render TH, not TD

        Table table = Controls[0] as Table;

        if (table != null && table.Rows.Count > 0)
        {
            table.Rows[0].TableSection = TableRowSection.TableHeader;
            table.Rows[table.Rows.Count - 1].TableSection = TableRowSection.TableFooter;
        }

        base.OnPreRender(e);
    }
}

答案 1 :(得分:4)

也可以通过javascript完成。

function AddTHEAD(tableName)
{
   var table = document.getElementById(tableName); 
   if(table != null) 
   {
    var head = document.createElement("THEAD");
    head.style.display = "table-header-group";
    head.appendChild(table.rows[0]);
    table.insertBefore(head, table.childNodes[0]); 
   }
}

然后你必须在body onload上调用这个函数:

<body onload="javascript: AddTHEAD('DataGridId')">

来源:http://www.codeproject.com/KB/grid/HeaderOnEachPage.aspx

答案 2 :(得分:0)

DataGrid没有内置功能来满足您的需求。看看ASP.NET 2.0 CSS Friendly Control Adapters 1.0他们对DataView有内置的支持,但似乎你可以轻松地将这个想法用于DataGrid。

答案 3 :(得分:0)

是的,看起来数据网格不支持开箱即用,所以我必须创建一个继承自DataGrid的类。在DataGrid呈现之后,我然后解析HTML并将元素注入正确的位置。

对于那些想知道如何的人来说,这是我的课程。这是一种快速而肮脏的方法,所以我欢迎更好的想法。


Imports System.IO
Imports System.Text

Public Class TestDataGrid
  Inherits System.Web.UI.WebControls.DataGrid

  Private sTHeadClass As String = String.Empty
  Private sTBodyClass As String = String.Empty
  Private sTFootClass As String = String.Empty

#Region " Properties "

  Public Property THeadClass() As String
    Get
      Return sTHeadClass
    End Get
    Set(ByVal value As String)
      sTHeadClass = value
    End Set
  End Property

  Public Property TBodyClass() As String
    Get
      Return sTBodyClass
    End Get
    Set(ByVal value As String)
      sTBodyClass = value
    End Set
  End Property

  Public Property TFootClass() As String
    Get
      Return sTFootClass
    End Get
    Set(ByVal value As String)
      sTFootClass = value
    End Set
  End Property

#End Region

  Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

    Dim oMemoryStream As New MemoryStream()
    Dim oStreamWriter As New StreamWriter(oMemoryStream)
    Dim oStreamReader As New StreamReader(oMemoryStream)
    Dim oHtmlTextWriter As New HtmlTextWriter(oStreamWriter)

    MyBase.Render(oHtmlTextWriter)

    oHtmlTextWriter.Flush()

    oMemoryStream.Flush()
    oMemoryStream.Position = 0

    Dim sHtml As String = oStreamReader.ReadToEnd()
    Dim oHtml As New Text.StringBuilder()

    Dim iPastIndex As Integer = 0
    Dim iIndex As Integer = sHtml.IndexOf("<tr>")

    oHtml.Append(sHtml.Substring(iPastIndex, iIndex - iPastIndex))

    iPastIndex = iIndex

    If ShowHeader Then
      WriteElementStart(oHtml, "thead", sTHeadClass)

      'Write Header Row
      iIndex = sHtml.IndexOf("</tr>", iPastIndex) + 5
      oHtml.Append(sHtml.Substring(iPastIndex, iIndex - iPastIndex))
      iPastIndex = iIndex

      oHtml.Append("</thead>")
      WriteElementStart(oHtml, "tbody", sTBodyClass)
    Else
      WriteElementStart(oHtml, "tbody", sTBodyClass)
    End If

    If ShowFooter Then

      'Writer Body Rows
      iIndex = sHtml.LastIndexOf("<tr>")
      oHtml.Append(sHtml.Substring(iPastIndex, iIndex - iPastIndex))
      iPastIndex = iIndex

      WriteElementEnd(oHtml, "tbody")
      WriteElementStart(oHtml, "tfoot", sTFootClass)

      'Write Footer Row
      iIndex = sHtml.LastIndexOf("</table>")
      oHtml.Append(sHtml.Substring(iPastIndex, iIndex - iPastIndex))
      iPastIndex = iIndex

      WriteElementEnd(oHtml, "tfoot")

    Else
      iIndex = sHtml.LastIndexOf("</table>")
      oHtml.Append(sHtml.Substring(iPastIndex, iIndex - iPastIndex))
      iPastIndex = iIndex

      WriteElementEnd(oHtml, "tbody")
    End If

    oHtml.Append(sHtml.Substring(iPastIndex, sHtml.Length - iPastIndex))

    writer.Write(oHtml.ToString())
  End Sub

  Private Sub WriteElementStart(ByVal Builder As StringBuilder, ByVal Tag As String, ByVal CssClass As String)
    If String.IsNullOrEmpty(CssClass) Then
      Builder.AppendFormat("<{0}>", Tag)
    Else
      Builder.AppendFormat("<{0} class='{1}'>", Tag, CssClass)
    End If
  End Sub

  Private Sub WriteElementEnd(ByVal Builder As StringBuilder, ByVal Tag As String)
    Builder.AppendFormat("</{0}>", Tag)
  End Sub

End Class