从 ASP.net 页面上的集合输出数据的最佳实践是什么?

发布于 2024-08-29 18:22:32 字数 938 浏览 5 评论 0原文

我已将一个页面从经典 ASP 移植到 ASP.net。此页面中发生的部分情况是生成自定义类型的集合,然后通过 Response.Write() 命令显示。我希望将业务逻辑分离到代码隐藏文件中(并且可能将其全部移至用户控件中),但我似乎无法弄清楚在生成集合后如何实际显示该集合。我也想在这里指定一个母版页,这样代码就不能保持内联。这是当前代码的一个非常精简的版本:

<%
Dim objs as ArrayList = New ArrayList()

For i = 0 To 2
    Dim obj as Obj = New Obj()

obj.setProp1("ASDF")
obj.setProp2("FDSA")

objs.Add(obj)
Next i
%>

<table>
<thead>
    <tr>
        <th scope="col">Property 1</th>
        <th scope="col">Property 2</th>
    </tr>
</thead>
<tbody>
<%
For Each obj As Obj In objs
Dim objProp1 As String = obj.getProp1
Dim objProp2 As String = obj.getProp2
%>                  
    <tr>
        <td><% Response.Write(objProp1)%></td>
        <td><% Response.Write(objProp2)%></td>
    </tr>
<%
Next
%>
</tbody>
</table>

“.net”的执行方式是什么?

I've ported a page from classic ASP to ASP.net. Part of what happens in this page is that a collection of custom types is generated and then displayed via Response.Write() commands. I'd like to get the business logic separated out into a code behind file (and maybe move this all into a user control), but I can't seem to figure out how I'd actually display the collection once it's been generated. I want to specify a master page here, too, so the code can't stay inline. Here's a very stripped down version of the current code:

<%
Dim objs as ArrayList = New ArrayList()

For i = 0 To 2
    Dim obj as Obj = New Obj()

obj.setProp1("ASDF")
obj.setProp2("FDSA")

objs.Add(obj)
Next i
%>

<table>
<thead>
    <tr>
        <th scope="col">Property 1</th>
        <th scope="col">Property 2</th>
    </tr>
</thead>
<tbody>
<%
For Each obj As Obj In objs
Dim objProp1 As String = obj.getProp1
Dim objProp2 As String = obj.getProp2
%>                  
    <tr>
        <td><% Response.Write(objProp1)%></td>
        <td><% Response.Write(objProp2)%></td>
    </tr>
<%
Next
%>
</tbody>
</table>

What is the ".net" way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小情绪 2024-09-05 18:22:32

您还可以查看 ListView 控件,它是一个较新的控件Joe R 提到的中继器版本。 ScottGu 的博客

您的代码基本上会变成以下内容:

<asp:ListView id="ListView1" runat="server" enableviewstate="false">
    <LayouTemplate>
        <table>
            <thead>
                <tr>
                    <th scope="col">Property 1</th>
                    <th scope="col">Property 2</th>
                 </tr>
             </thead>
             <tbody>
                 <asp:Placeholder runat="server" id="ItemPlaceholder" />
             </tbody>
        </table>
    </LayouTemplate>
    <ItemTemplate>
        <tr>
            <td><%# Eval("objProp1" )%></td>
            <td><%# Eval("objProp2" )%></td>
        </tr>
    </ItemTemplate>
</asp:ListView>

这些人考虑使用Eval< /code> 这不是一个好的做法,但它使编写示例变得更容易。如果您呈现只读数据,请不要忘记关闭 ViewState,否则您的页面会很快变得非常大。

编辑还发现了不同列表样式控件之间的功能比较表

You can also take a look at the ListView control which is a newer version of the repeater that Joe R mentioned. There's a great tutorial on what you can do with a ListView on ScottGu's blog.

Your code would basically turn into something along these lines:

<asp:ListView id="ListView1" runat="server" enableviewstate="false">
    <LayouTemplate>
        <table>
            <thead>
                <tr>
                    <th scope="col">Property 1</th>
                    <th scope="col">Property 2</th>
                 </tr>
             </thead>
             <tbody>
                 <asp:Placeholder runat="server" id="ItemPlaceholder" />
             </tbody>
        </table>
    </LayouTemplate>
    <ItemTemplate>
        <tr>
            <td><%# Eval("objProp1" )%></td>
            <td><%# Eval("objProp2" )%></td>
        </tr>
    </ItemTemplate>
</asp:ListView>

These guys consider using Eval to not be a good practice, but it made writing the example easier. If you're presenting read only data, don't forget to turn off ViewState or your pages will get very big very quickly.

EDIT Also found a feature comparison chart between the different list style controls here.

梦里梦着梦中梦 2024-09-05 18:22:32

在.NET2 中,您最好选择中继器。像这样的东西:

<asp:Repeater id="rpt1" runat="server" EnableViewState="false"> 
    <HeaderTemplate> 
        <table> 
            <thead> 
                <tr> 
                    <th scope="col">Property 1</th> 
                    <th scope="col">Property 2</th> 
                 </tr> 
             </thead> 
             <tbody> 
    </HeaderTemplate>
    <ItemTemplate>
                 <tr> 
                     <td><%# Eval("objProp1" )%></td> 
                     <td><%# Eval("objProp2" )%></td> 
                 </tr>
    </ItemTemplate>
    <FooterTemplate>
             </tbody> 
        </table> 
    </FooterTemplate>
</asp:Repeater> 

在你后面的代码中绑定这样的数据:

rpt1.DataSource = objs;
rpt1.DataBind();

这将是c#,但希望没关系

In .NET2 your best bet it the repeater. Something like this:

<asp:Repeater id="rpt1" runat="server" EnableViewState="false"> 
    <HeaderTemplate> 
        <table> 
            <thead> 
                <tr> 
                    <th scope="col">Property 1</th> 
                    <th scope="col">Property 2</th> 
                 </tr> 
             </thead> 
             <tbody> 
    </HeaderTemplate>
    <ItemTemplate>
                 <tr> 
                     <td><%# Eval("objProp1" )%></td> 
                     <td><%# Eval("objProp2" )%></td> 
                 </tr>
    </ItemTemplate>
    <FooterTemplate>
             </tbody> 
        </table> 
    </FooterTemplate>
</asp:Repeater> 

and in your code behind you bind the data like this:

rpt1.DataSource = objs;
rpt1.DataBind();

This would be c# though - hope that's okay

锦上情书 2024-09-05 18:22:32

我会使用中继器或数据网格之类的东西并将其绑定到 List<>您可以在业务逻辑层中创建。

I would use something like a repeater or datagrid and bind it to a List<> that you can create in your Business Logic layer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文