2009-02-18 54 views

Trả lời

0

DataGrid không có thứ gì đó được tích hợp sẵn để đáp ứng nhu cầu của bạn. Hãy xem trên ASP.NET 2.0 CSS Friendly Control Adapters 1.0 chúng được hỗ trợ sẵn cho DataView, nhưng có vẻ như bạn có thể dễ dàng áp dụng ý tưởng này cho DataGrid.

+0

Chế độ xem GridView nhưng không phải là dữ liệu. –

+0

Đây chỉ là một ý tưởng. Tương đương với "Sử dụng bộ điều hợp điều khiển" nhưng với mẫu. Bởi vì gói này là các nguồn mở. Không nhiều. –

0

Phải, có vẻ như lưới dữ liệu không hỗ trợ điều này ra khỏi hộp, vì vậy tôi phải tạo một lớp kế thừa từ DataGrid. Sau khi DataGrid đã hiển thị, tôi phân tích cú pháp HTML và chèn các phần tử vào đúng vị trí.

Kèm theo là lớp học của tôi cho những người thích biết làm thế nào. Đây là một cách tiếp cận nhanh chóng và dơ bẩn, vì vậy tôi được chào đón với những ý tưởng tốt hơn.


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 
+0

Xin lỗi, nhưng tôi không thích giải pháp này. Ví dụ, nó phụ thuộc vào HtmlWriter được sử dụng. Với UpperCaseHtmlWriter, giải pháp của bạn sẽ không hoạt động. Ngoài ra mã với MemoryStream ... Ngoài ra HtmlTextWriter ... –

+0

Bạn có thể giải thích thêm? Phương thức Render của điều khiển mở rộng một HtmlTextWriter làm đầu vào, nó nằm trong định nghĩa! Phản đối của bạn đối với MemoryStream, v.v. – Ady

3

Cũng có thể thực hiện qua 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]); 
    } 
} 

Sau đó, bạn phải gọi chức năng này trên onload thân hình như vậy:

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

Nguồn: http://www.codeproject.com/KB/grid/HeaderOnEachPage.aspx

+0

Một giải pháp javascript Nice one! –

8

Trong khi tôi thích câu trả lời bằng cách "user186197", rằng bài viết trên blog sử dụng phản ánh, mọi thứ có thể gặp sự cố trong môi trường lưu trữ không đáng tin cậy. Dưới đây là những gì chúng tôi sử dụng, không có hack:

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); 
    } 
} 
+0

Tôi thích giải pháp này, với một vài thay đổi nhỏ: 'if (this.ShowHeader) table.Rows [0] .TableSection = TableRowSection.TableHeader;' và 'if (this.ShowFooter) table.Rows [table.Rows.Count - 1] .TableSection = TableRowSection.TableFooter; ' –

+0

@JohnAllers làm thế nào chúng ta có thể sử dụng lớp này trong hiện tại bạn có thể mô tả tôi ..? –

+2

Trong khi câu trả lời này hoạt động và tôi đánh giá cao nó, đây là loại công cụ khiến tôi cảm thấy chán nản về các biểu mẫu web và cuối cùng đã đẩy tôi tới MVC. – akousmata

Các vấn đề liên quan