如何在ASP.NET中最好地填充HTML表?

前端之家收集整理的这篇文章主要介绍了如何在ASP.NET中最好地填充HTML表?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这就是我所拥有的.有用.但是,有更简单或更好的方法吗?

ASPX页面……

ASPX.VB代码背后……

Protected Sub Page_Load( ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Load
    Dim db As New BookstoreDataContext
    RepeaterBooks.DataSource = From b In db.Books _
                               Order By b.Published _
                               Select b
    RepeaterBooks.DataBind()
End Sub

Sub RepeaterBooks_ItemDataBound( ByVal sender As Object,ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles RepeaterBooks.ItemDataBound
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim b As Book = DirectCast(e.Item.DataItem,Book)
        DirectCast(e.Item.FindControl("LiteralPublished"),Literal).Text = "
最佳答案
@Geoff

那种Eval语句实际上是在2.0中添加的,但是如果性能很重要,应避免使用Eval,因为它使用了Reflection.

转发器是一种非常好的方法,尽管在代码生成表可能更快:

ASPX页面

代码背后:

Protected Sub Page_Load( ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostback Then
        BuildTable()
    End If
End Sub

Private Sub BuildTable()
    Dim db As New BookstoreDataContext
    Dim bookCollection = from b in db.Books _
                         Order By b.Published _
                         Select b
    Dim row As HtmlTableRow
    Dim cell As HtmlTableCell

    For Each book As Books In bookCollection
        row = New HtmlTableRow()
        cell = New HtmlTableCell With { .InnerText = b.Published.ToShortDateString }
        row.Controls.Add(cell)
        cell = New HtmlTableCell With { .InnerText = TryNbsp(HttpContext.Current.Server.HtmlEncode(b.Title)) }
        row.Controls.Add(cell)
        cell = New HtmlTableCell With { .InnerText = TryNbsp(HttpContext.Current.Server.HtmlEncode(b.Author))
        row.Controls.Add(cell)
        cell = New HtmlTableCell With { .InnerText = Format(b.Price,"c") }
        row.Controls.Add(cell)
        bookTable.Controls.Add(row)
    Next

我想这取决于速度对你的重要程度.为简单起见,我想我会选择Repeater. 原文链接:https://www.f2er.com/html/425862.html

猜你在找的HTML相关文章