Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在ASP.NET中最好地填充HTML表?_Asp.net_Html_Vb.net - Fatal编程技术网

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

如何在ASP.NET中最好地填充HTML表?,asp.net,html,vb.net,Asp.net,Html,Vb.net,这就是我得到的。它起作用了。但是,有更简单或更好的方法吗 ASPX页面 <asp:Repeater ID="RepeaterBooks" runat="server"> <HeaderTemplate> <table class="report"> <tr> <th>Published</th> <th>

这就是我得到的。它起作用了。但是,有更简单或更好的方法吗

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>

出版
标题
作者
价格
ASP.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
Protected Sub Page_Load(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Load
Dim db作为新书店DataContext
RepeaterBooks.DataSource=来自db.Books中的b_
b.发布的命令_
选择b
RepeaterBooks.DataBind()
端接头
子RepeaterBooks_ItemDataBound(ByVal sender作为对象,ByVal e作为System.Web.UI.WebControls.RepeaterItemEventArgs)处理RepeaterBooks.ItemDataBound
如果e.Item.ItemType=ListItemType.Item或e.Item.ItemType=ListItemType.AlternatingItem,则
Dim b As Book=DirectCast(e.Item.DataItem,Book)
DirectCast(e.Item.FindControl(“LiteralPublished”),Literal.Text=“+b.Published.ToShortDateString+”)
DirectCast(e.Item.FindControl(“LiteralTitle”),Literal.Text=“+TryNbsp(HttpContext.Current.Server.HtmlEncode(b.Title))+”“
DirectCast(e.Item.FindControl(“LiteralAuthor”),Literal.Text=”“+TryNbsp(HttpContext.Current.Server.HtmlEncode(b.Author))+“”
DirectCast(e.Item.FindControl(“LiteralPrice”),Literal.Text=”“+格式(b.Price,“c”)+“”
如果结束
端接头
函数TryNbsp(ByVal s作为字符串)作为字符串
如果s=”“,则
返回“”
其他的
返回s
如果结束
端函数

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

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

...
其中“published”是绑定到中继器的数据中的字段名称

编辑:
@当前位置我认为反思的表演效果往往被过分强调。显然,您需要对应用程序的性能进行基准测试,但评估的命中率可能以毫秒为单位。除非你的应用程序同时提供很多点击,否则这可能不是一个问题,使用Eval的代码的简单性,加上它是演示文稿的一个很好的分离,使它成为一个很好的解决方案。

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

@Geoff

这种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>
我想这取决于速度对你有多重要。为了简单起见,我想我会选择中继器

写道:

…在代码中生成表

我喜欢它的样子!由于输入错误或字段名更改,似乎不太可能产生运行时异常。

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

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


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

这就是GridView的用途

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

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


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

如果您不需要ASP.NET处理的编辑功能,我将远离数据网格和GridView。。。它们提供了不必要的膨胀

<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