Table控件绑定类
来源:互联网 发布:资产重组 知乎 编辑:程序博客网 时间:2024/05/29 10:55
在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);
}
}
}
}
- Table控件绑定类
- Table控件
- XML绑定到Table
- MAPX绑定TABLE
- Repeater 与 Table 绑定
- WPF DataGrid 绑定Table
- Table绑定数据源
- bootstrap table 绑定数据
- DataTable绑定Table控件中的数据(vb.net之经典应用)
- 控件模型绑定控件
- 用于绑定asp.net控件的类
- vc自定义控件及类绑定
- 自定义类 绑定到控件 更新控件内容
- table伪动态数据绑定
- div与table事件绑定
- 表控件(Table)
- C# table控件操作
- c# table 控件用法
- Spring事务类型祥解
- 测试
- Android学习笔记(6)-关于Dialog的简单体验
- 我想编写CSharp程序
- 粗略的博客需求
- Table控件绑定类
- 构建基于通用导航器架构(CNF)的视图 第一部分:定义ViewPart
- 第二人生的源码分析(2)第二人生的基本功能
- 揭开木马的神秘面纱
- 第七課 文法
- Magic Linux 2.0 引导文件 GRUB 修复
- 网管员必知:常用电脑密码破解
- 智能DNS
- 水晶报表自定义打印非完美解决方案