加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

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

发布时间:2020-12-14 22:47:48 所属栏目:资源 来源:网络整理
导读:这就是我所拥有的.有用.但是,有更简单或更好的方法吗? 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

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

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!