<%@ 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./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www./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>
后臺(tái)文件:
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()
{
//獲取數(shù)據(jù)集
DataTable tableSource = getTable();
if (null == tableSource)
{
return;
}
//創(chuàng)建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;
//第一步:將表格的標(biāo)題獲取出來
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);
}
//第二步:添加表格的內(nèi)容,,遍歷數(shù)據(jù)集
foreach (DataRow item in tableSource.Rows)
{
//創(chuàng)建新的行
rowHTML = new HtmlTableRow();
//添加特效事件
rowHTML.Attributes.Add("onmouseover", "this.className='table_tr_Over'");
rowHTML.Attributes.Add("onmouseout", "this.className='table_tr_Out'");
//依次循環(huán)tableSource中某一行的每一列,跟蹤一下就明白
for (int i = 0; i < tableSource.Columns.Count; i++)
{
//創(chuàng)建新的元素
AnchorHTML = new HtmlAnchor();
AnchorHTML.HRef = "Index.aspx";
//可根據(jù)item[i]或item["Name"]來獲取數(shù)據(jù)
AnchorHTML.InnerHtml = item[tableSource.Columns[i].ToString()].ToString();
//創(chuàng)建新的單元格
cellHTML = new HtmlTableCell();
//添加元素
cellHTML.Controls.Add(AnchorHTML);
//添加單元格
rowHTML.Cells.Add(cellHTML);
}
//向HTML表格中添加行
tableHTML.Rows.Add(rowHTML);
}
//向PlaceHolder容器中添加表格
plhTable.Controls.Add(tableHTML);
}
/// <summary>
/// 獲取數(shù)據(jù)集
/// </summary>
/// <returns></returns>
private DataTable getTable()
{
//創(chuàng)建表
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;
}
}
}