【问题标题】:How do I best populate an HTML table in ASP.NET?如何最好地在 ASP.NET 中填充 HTML 表?
【发布时间】:2010-09-07 19:30:03
【问题描述】:

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

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

【问题讨论】:

    标签: asp.net html vb.net


    【解决方案1】:

    @杰夫

    这种 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
    

    我想这取决于速度对您来说有多重要。为简单起见,我想我会选择中继器。

    【讨论】:

      【解决方案2】:

      框架 3.5 引入的 ListView 控件可能是更好的解决方案。您的标记将如下所示:

      <asp:ListView runat="server" ID="ListView1"
          DataSourceID="SqlDataSource1">
        <LayoutTemplate>
          <table runat="server" id="table1" runat="server" >
            <tr runat="server" id="itemPlaceholder" ></tr>
          </table>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td runat="server">
              <asp:Label ID="NameLabel" runat="server"
                Text='<%#Eval("Name") %>' />
            </td>
          </tr>
        </ItemTemplate>
      </asp:ListView>
      

      您需要通过代码隐藏类中的公共或私有属性设置数据源 ID。

      【讨论】:

        【解决方案3】:

        在 .Net 3.0+ 中,您可以通过执行以下操作将 ItemDataBound 替换为 asp:Literal:

        <ItemTemplate>
                    <tr>
                        <td><%# Eval("published") %></td>
                        ...
        

        其中“已发布”是您绑定到转发器的数据中的字段名称

        编辑: @Alassek:我认为反射对性能的影响往往被过分强调了。显然,您需要对应用程序的性能进行基准测试,但 Eval 的命中率可能以毫秒为单位。除非您的应用程序提供许多并发点击,否则这可能不是问题,并且使用 Eval 的代码的简单性,以及它很好地分离表示,使其成为一个很好的解决方案。

        【讨论】:

          【解决方案4】:

          这就是 GridView 的用途。

          <asp:GridView runat="server" DataSourceID="SqlDataSource1">
             <Columns>
                <asp:BoundField HeaderText="Published" DataField="Published" />
                <asp:BoundField HeaderText="Author" DataField="Author" />
             </Columns>
          </asp:GridView>
          

          【讨论】:

            【解决方案5】:

            我会使用 GridView(或 DataGrid,如果您使用的是旧版本的 ASP.NET)。

            <asp:GridView ID="gvBooks" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField HeaderText="Published" DataField="Published" />
                    <asp:BoundField HeaderText="Title" DataField="Title" />                     
                    <asp:BoundField HeaderText="Author" DataField="Author" />
                    <asp:BoundField HeaderText="Price" DataField="Price" />
                </Columns>
            </asp:GridView>
            

            有一些代码隐藏:

            Private Sub gvBooksRowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvBooks.RowDataBound
                 Select Case e.Row.RowType
                    Case DataControlRowType.DataRow
            
                        ''' Your code here '''
            
                 End Select
            End Sub
            

            你可以用类似的方式绑定它。 RowDataBound 事件就是您所需要的。

            【讨论】:

              【解决方案6】:

              我同意 Geoff 的观点,我们唯一使用 Literals 是如果我们想对数据做一些不同的事情。
              例如,我们可能希望 DueDate 字段显示“今天”或“昨天”而不是实际日期。

              【讨论】:

                【解决方案7】:

                ALassek 写道:

                …用代码生成表格…

                我喜欢它的外观!由于拼写错误或字段名称更改,似乎不太可能产生运行时异常。

                【讨论】:

                  【解决方案8】:

                  如果您不需要 ASP.NET 处理的编辑功能,我会远离 DataGrid 和 GridView ...它们会提供不必要的膨胀

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2014-09-14
                    • 2021-11-17
                    相关资源
                    最近更新 更多