由于DataList GridView 等性能及样式问题,有的同事希望用 ul li 或者 Table 来实现绑定 数据源
以下为实例
前台页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>前台绑定后台 泛型 List DEMO</div>
<div>
<table>
<%foreach (Entity x in list)
{
%>
<tr>
<td><%=x.ID%></td><td><%=x.Name %></td>
</tr>
<%} %>
</table>
</div>
</form>
</body>
</html>
后置代码:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
public List<Entity> list = new List<Entity>();//注意为 public
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
list.Add(new Entity(1,"lee"));
list.Add(new Entity(2, "jin"));
list.Add(new Entity(3, "leejin"));
}
}
}
public class Entity
{
public Entity() { }
public Entity(int id, string name)
{
this._ID = id;
this._Name = name;
}
private int _ID;
public int ID
{
get { return _ID; }
set { _ID = value; }
}
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
}