我同意这里的每个人的观点,您应该使用 GridView,现在看一下这个快速示例以了解如何使用它,您几乎可以将表格格式化为您想要的任何方式(而且您实际上不必输入所有你自己的代码VS会为你做很多事情)
.ASPX
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
。CS
public class News
{
public int id { get; set; }
public string title { get; set; }
public string content { get; set; }
//as many properties as you want
}
public partial class bindGridviewToList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
News n1 = new News() { id = 1, content = "content", title = "title" };
News n2 = new News() { id = 2, content = "content", title = "title" };
News n3 = new News() { id = 3, content = "content", title = "title" };
List<News> newsList = new List<News>();
newsList.Add(n1); newsList.Add(n2); newsList.Add(n3);
GridView1.DataSource = newsList;
GridView1.DataBind();
}
}
运行此示例,您将看到使用 GridView 是多么容易