table循环显示数据
一直都在找一个关于table循环显示数据的应用,可找来找去,最终还是自己把它给做出来了,为了记得更深刻,现将它记录下来:
前台代码:
代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebTable.aspx.cs" Inherits="WebAppTable.WebTable" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
td
{
border: 1px solid #C0C0C0;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
数据显示(Table):<br />
<table style="font-size: 12px; background-color: #00FFFF; text-align: center;">
<tr><td>姓名</td><td>年龄</td></tr>
<%
int Count=GetList().Count;
for (int i = 0; i < Count; i++)
{
%>
<tr><td><%=GetList()[i].Name %></td><td><% Response.Write(GetList()[i].Age); %></td></tr>
<%}
%>
</table>
</div>
</form>
</body>
</html>
后台的代码如下:
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//using ConsoleApplication_Generic;
namespace WebAppTable
{
public partial class WebTable : System.Web.UI.Page
{
int Count;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Count = GetList().Count;
//Response.Write("<table><tr><td>姓名</td> <td>年龄</td></tr>");
//for (int i = 0; i < Count; i++)
//{
// Response.Write("<tr><td>" + GetList()[i].Name + "</td> <td>" + GetList()[i].Age + "</td></tr>");
//}
//Response.Write("</table>");
}
}
/// <summary>
/// 返回泛型集合列表
/// </summary>
/// <param name="emplist"></param>
/// <returns></returns>
public List<Emp> GetList()
{
List<Emp> emplist = new List<Emp>();
emplist.Add(new Emp("十九", 19));
emplist.Add(new Emp("二十", 20));
emplist.Add(new Emp("二十一", 21));
emplist.Add(new Emp("二十二", 22));
emplist.Add(new Emp("二十三", 23));
return emplist;
}
}
public class Emp
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
public Emp()
{ }
public Emp(string Name, int Age)
{
name = Name;
age = Age;
}
}
}