ASP.NET中用后台代码创建DataTable和HTML表格
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebHTML_Table._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>HTML表格测试</title>
<style type="text/css">
tr
{
line-height:22px;
}
A
{
color:#669966;
}
.table_tr_Over
{
background-color:#F6F6F6;
}
.table_tr_Out
{
background-color:#FFFFFF;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="plhTable" runat="server"></asp:PlaceHolder>
</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 System.Data;
using System.Web.UI.HtmlControls;
namespace WebHTML_Table
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Initialize();
}
}
/// <summary>
/// 初始化
/// </summary>
private void Initialize()
{
//获取数据集
DataTable tableSource = getTable();
if (null == tableSource)
{
return;
}
//创建HTML表格
HtmlTable tableHTML = new HtmlTable();
//初始化表格
tableHTML.Width = "100%";
tableHTML.CellPadding = 0;
tableHTML.CellSpacing = 0;
tableHTML.Border = 1;
//HTML行
HtmlTableRow rowHTML = new HtmlTableRow();
//HTML单元格
HtmlTableCell cellHTML = new HtmlTableCell();
//HTML<A>元素
HtmlAnchor AnchorHTML;
//第一步:将表格的标题获取出来
for (int i = 0; i < tableSource.Columns.Count; i++)
{
cellHTML = new HtmlTableCell();
cellHTML.Align = "center";
cellHTML.VAlign = "middle";
cellHTML.InnerHtml = "<b>" + tableSource.Columns[i].ToString() + "</b>";
rowHTML.Cells.Add(cellHTML);
tableHTML.Rows.Add(rowHTML);
}
//第二步:添加表格的内容,遍历数据集
foreach (DataRow item in tableSource.Rows)
{
//创建新的行
rowHTML = new HtmlTableRow();
//添加特效事件
rowHTML.Attributes.Add("onmouseover", "this.className='table_tr_Over'");
rowHTML.Attributes.Add("onmouseout", "this.className='table_tr_Out'");
//依次循环tableSource中某一行的每一列,跟踪一下就明白
for (int i = 0; i < tableSource.Columns.Count; i++)
{
//创建新的元素
AnchorHTML = new HtmlAnchor();
AnchorHTML.HRef = "Index.aspx";
//可根据item[i]或item["Name"]来获取数据
AnchorHTML.InnerHtml = item[tableSource.Columns[i].ToString()].ToString();
//创建新的单元格
cellHTML = new HtmlTableCell();
//添加元素
cellHTML.Controls.Add(AnchorHTML);
//添加单元格
rowHTML.Cells.Add(cellHTML);
}
//向HTML表格中添加行
tableHTML.Rows.Add(rowHTML);
}
//向PlaceHolder容器中添加表格
plhTable.Controls.Add(tableHTML);
}
/// <summary>
/// 获取数据集
/// </summary>
/// <returns></returns>
private DataTable getTable()
{
//创建表
DataTable tableInfo = new DataTable();
//列
DataColumn dcName = new DataColumn("Name", Type.GetType("System.String"));
DataColumn dcEmail = new DataColumn("Email", Type.GetType("System.String"));
DataColumn dcAddress = new DataColumn("Address", Type.GetType("System.String"));
//添加列
tableInfo.Columns.Add(dcName);
tableInfo.Columns.Add(dcEmail);
tableInfo.Columns.Add(dcAddress);
for (int i = 0; i < 10; i++)
{
//行
DataRow dr = tableInfo.NewRow();
dr["Name"] = "Name" + i.ToString();
dr["Email"] = "Email" + i.ToString();
dr["Address"] = "Address" + i.ToString();
//添加行
tableInfo.Rows.Add(dr);
}
return tableInfo;
}
}
}