What is the best practice for outputting data from a collection on an ASP.net page?

I ported the page from classic ASP to ASP.net. Part of what happens on this page is that a collection of custom types is created and then displayed through commands Response.Write(). I would like the business logic to be separated from the code behind the file (and possibly move all this to a user control), but I cannot understand how I actually display the collection after it is created. Here I also want to specify the main page, so the code cannot remain 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" method?

+3
3

ListView, , . , ListView 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, , . , ViewState, .

.

+2

.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();

#, - ,

+2

I would use something like a repeater or datagrid and bind it to a <> list, which can be created at the level of your business logic.

0
source

Source: https://habr.com/ru/post/1741992/


All Articles