Có cách nào để có được điều khiển DataGrid để hiển thị các phần tử tbody và thead HTML không?ASP.NET 2.0 - DataGrid với tbody/thead
Trả lời
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.
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
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 ... –
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
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
Một giải pháp javascript Nice one! –
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);
}
}
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; ' –
@JohnAllers làm thế nào chúng ta có thể sử dụng lớp này trong
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
- 1. ASP.NET 2.0 JQuery AJAX Đăng nhập
- 2. Sử dụng Url.Content trong ASP.net MVC 2.0
- 3. ASP.NET MVC 2.0 JsonRequestBehavior Global Setting
- 4. DataGrid nào được sử dụng cho dự án ASP.NET MVC2?
- 5. Làm thế nào để xóa một DataGrid ASP.NET?
- 6. Datagrid vs GridView
- 7. SAML với .NET 2.0
- 8. NServiceBus với Unity 2.0?
- 9. Điều gì có nghĩa là ASP.net Phiên bản 2.0?
- 10. Điền vào DataGrid với các hàng trống
- 11. WPF DataGrid với chiều cao hàng biến
- 12. Cài đặt chính xác của ClientIDMode trong ASP.Net 4 là gì để hiển thị ASP.Net 2.0.
- 13. Asp.net Mvc 2.0 RC Thiết lập Chủ đề CurrentCulture/Ui
- 14. ASP.NET 2.0 thêm kiểu động vào trang trong điều khiển
- 15. Tài nguyên cho việc học ASP.NET MVC 2.0
- 16. ASP.NET MVC 2.0 - Sự khác biệt giữa RenderPartial và RenderAction
- 17. Jackson 2.0 với Spring 3.1
- 18. WPF horizontal DataGrid
- 19. WPF DataGrid RowHeader databinding
- 20. WPF DataGrid SelectedItem
- 21. Vấn đề Wpf DataGrid
- 22. Ràng buộc với một lệnh trong một DataGrid
- 23. WPF: Liên kết DataGrid với Danh sách <String>
- 24. WPF DataGrid Tiêu đề Cột Resize với Custom Phong cách
- 25. Wpf DataGrid ClipboardCopyMode = "IncludeHeader" với tiêu đề tùy chỉnh
- 26. hiệu suất chậm với WPF DataGrid và ScrollViewer
- 27. OpenGL ES 2.0 tương đương với glOrtho()?
- 28. Couchbase 2.0 so với Couchbase 1.8?
- 29. Cách tạo WSDL 2.0 với JAX-WS
- 30. Sử dụng Eclipse với Khung chơi 2.0
Chế độ xem GridView nhưng không phải là dữ liệu. –
Đâ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. –