从 ASP.net 页面上的集合输出数据的最佳实践是什么?
我已将一个页面从经典 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以查看 ListView 控件,它是一个较新的控件Joe R 提到的中继器版本。 ScottGu 的博客。
您的代码基本上会变成以下内容:
这些人考虑使用
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:
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.
在.NET2 中,您最好选择中继器。像这样的东西:
在你后面的代码中绑定这样的数据:
这将是c#,但希望没关系
In .NET2 your best bet it the repeater. Something like this:
and in your code behind you bind the data like this:
This would be c# though - hope that's okay
我会使用中继器或数据网格之类的东西并将其绑定到 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.