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

标签 asp.net html vb.net

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

ASPX 页面...

<asp:Repeater ID="RepeaterBooks" runat="server">
    <HeaderTemplate>
        <table class="report">
            <tr>
                <th>Published</th>
                <th>Title</th>
                <th>Author</th>
                <th>Price</th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
            <tr>
                <td><asp:Literal ID="LiteralPublished" runat="server" /></td>
                <td><asp:Literal ID="LiteralTitle" runat="server" /></td>
                <td><asp:Literal ID="LiteralAuthor" runat="server" /></td>
                <td><asp:Literal ID="LiteralPrice" runat="server" /></td>
            </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

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 = "<nobr>" + b.Published.ToShortDateString + "</nobr>"
        DirectCast(e.Item.FindControl("LiteralTitle"), Literal).Text = "<nobr>" + TryNbsp(HttpContext.Current.Server.HtmlEncode(b.Title)) + "</nobr>"
        DirectCast(e.Item.FindControl("LiteralAuthor"), Literal).Text = "<nobr>" + TryNbsp(HttpContext.Current.Server.HtmlEncode(b.Author)) + "</nobr>"
        DirectCast(e.Item.FindControl("LiteralPrice"), Literal).Text = "<nobr>" + Format(b.Price, "c") + "</nobr>"
    End If
End Sub

Function TryNbsp(ByVal s As String) As String
    If s = "" Then
        Return "&nbsp;"
    Else
        Return s
    End If
End Function

最佳答案

@杰夫

这种 Eval 语句实际上是在 2.0 中添加的,但是如果性能很重要,则应避免使用 Eval,因为它使用反射。

转发器是一个很好的方法,尽管用代码生成表格可能更快:

ASPX 页面:

<table class="report" id="bookTable" runat="server">
        <tr>
            <th>Published</th>
            <th>Title</th>
            <th>Author</th>
            <th>Price</th>
        </tr>
 </table>

代码隐藏:

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。

关于asp.net - 如何最好地在 ASP.NET 中填充 HTML 表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43803/

相关文章:

c# - 如何摆脱命令执行过程中遇到的 fatal error 。在mysql中

c# - GridView 的内联编辑

html - 如何在网站上播放 .amr 文件?

vb.net - txtExample.Clear() 与 txtExample.Text = String.Empty

c# - 下载 asp.net 网站呈现的 html 代码

asp.net - IIS 404 自定义错误未按预期工作

javascript - 按一次链接时隐藏链接中的图像

javascript - 为什么我的链接会泄露?

vb.net - 如何设置 ErrorProvider 闪烁量?

java - 在 Android 中获取 XML 响应