从列表C#asp.net填写表格 [英] Fill a table from a list C# asp.net

查看:237
本文介绍了从列表C#asp.net填写表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Car 类,其中一些属性模型。我有一个列表 List< Car> carList = new List< Car>()),类 Car ,我想将此列表放入HTML标记中,但我不知道如何执行此操作。

I have a class Car, with some atributtes Model and Year. I have a list (List<Car> carList = new List<Car>()) of some objects of the class Car, and I want to put this list into an HTML tag , but I have no idea how to do this.

我有一个已经有<$的模板c $ c>< table> ,这就是我尝试使用< table> 而不是<的原因; asp:GridView>

I got a template that already has a <table>, that's why I'm trying to use <table> instead of <asp:GridView>.

我正在使用WebForms

I'm using WebForms

推荐答案

如果你开始使用< table> 而不是< asp:*> 控件,您可以在页面上手动构建标记。

If you are set on using a <table> and not any of the <asp:*> controls, you can manually build your markup on the page.

首先,您需要能够从标记中访问该对象。通过在代码隐藏中使其成为 protected (或 public )属性,页面可以访问它:

First, you need to be able to access the object from your markup. By making it a protected (or public) property on the code-behind, the page can access it:

protected IList<Car> CarList { get { return carList; } }

然后在页面(继承自代码隐藏类)中,您可以访问该属性建立标记。这样的事情:

Then in the page (which inherits from the code-behind class) you can access that property to build markup. Something like this:

<% foreach (var car in CarList) { %>

<% } %>

在那个循环里面就是你的行,外面就是你的餐具。这样的事情可能是:

Inside of that loop would be your rows, outside would be your table dressing. Something like this, perhaps:

<table>
  <tr>
    <th>Column Heading</th>
  </tr>
<% foreach (var car in CarList) { %>
  <tr>
    <td><%= car.SomeValue %></td>
  </tr>
<% } %>
</table>

这与类似如何在MVC中构建标记,所以你可以这样想。虽然WebForms并不常见,但通常建议按照它的使用方式使用框架。从长远来看,如果您开始使用WebForms,那么调整模板以使用服务器端控件而不是手动构建标记可能会更少。服务器端控件具有许多事件绑定和代码隐藏交互的额外好处。

This is similar to how one may construct markup in MVC, so you can think of it like that. Though it's not common for WebForms and it's often recommended to use the framework the way it's meant to be used. In the long run, if you're set on using WebForms, it may be less work to adjust the template to work with the server-side controls than to manually build the markup. The server-side controls have the added benefit of lots of event binding and code-behind interactions.

这篇关于从列表C#asp.net填写表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆