在CSDN上面看到一个提问关于Table怎么绑定类的,尽管可以自己编写代码实现,却远不如GridView(或者Repeater)绑定类好用。Table控件绑定类程序如下,由类文件Person.cs和TableBindClass.aspx两张页面组成,代码如下:
Person.cs文件:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
/// <summary>
/// Person 的摘要说明
/// </summary>
public class Person
{
//构造函数
public Person(string M_personname,string M_personsex)
{
personName = M_personname;
personSex = M_personsex;
}
//姓名
private string personName;
public string PersonName
{
get
{
return personName;
}
set
{
personName = value;
}
}
//性别
private string personSex;
public string PersonSex
{
get
{
return personSex;
}
set
{
personSex = value;
}
}
//实例化两个Person对象作为数据源,实际应用可以从数据库读取
public static List<Person> GetPerson()
{
List<Person> PersonList = new List<Person>();
Person FirstPerson = new Person("dreamjhy", "男");
Person SecondPerson = new Person("chris", "男");
PersonList.Add(FirstPerson);
PersonList.Add(SecondPerson);
return PersonList;
}
}
TableBindClass.aspx页面代码,非常简单,就工具箱-->标准拖入一个Table控件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TableBindClass.aspx.cs" Inherits="Test_TableBindClass" %>
<!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>
<asp:Table ID="Table1" runat="server" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" CellPadding="1" CellSpacing="1" GridLines="Both">
</asp:Table>
</div>
</form>
</body>
</html>
TableBindClass.aspx.cs代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
public partial class Test_TableBindClass : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//
TableBindData();
}
//Table控件绑定类列表
private void TableBindData()
{
List<Person> PersonList = Person.GetPerson();
//创建标题行
TableRow HeadRow =new TableRow();
Table1.Rows.Add(HeadRow);
//创建"姓名"标头
TableCell NameCell=new TableCell();
NameCell.Text="姓名";
HeadRow.Cells.Add(NameCell);
//创建"性别"标头
TableCell SexCell=new TableCell();
SexCell.Text="性别";
HeadRow.Cells.Add(SexCell);
//创建数据行
int PersonCount = PersonList.Count;
//如果类列表有数据
if(PersonCount!=0)
{
for (int i = 0; i < PersonCount; i++)
{
//每个对象创建一行
TableRow DataRow = new TableRow();
Table1.Rows.Add(DataRow);
//创建"姓名"单元格
TableCell NameDataCell = new TableCell();
NameDataCell.Text = PersonList[i].PersonName;
DataRow.Cells.Add(NameDataCell);
//创建"性别"单元格
TableCell SexDataCell = new TableCell();
SexDataCell.Text = PersonList[i].PersonSex;
DataRow.Cells.Add(SexDataCell);
}
}
}
}