还不完善,在空隙有字,待以后解决吧!
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="Modules_test2" %>
![](/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div id="divgrid" style="overflow-y: scroll; height: 200px; width: 300px;">
<asp:GridView ID="GridView1" runat="server" CellSpacing="1" CellPadding="3" Font-Size="12px"
Width="600px" BorderWidth="0" OnRowDataBound="GridView1_RowDataBound"
OnRowCreated="GridView1_RowCreated" ShowHeader="false">
</asp:GridView>
</div>
</form>
</body>
</html>
![](/Images/OutliningIndicators/None.gif)
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;
![](/Images/OutliningIndicators/None.gif)
public partial class Modules_test2 : System.Web.UI.Page
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
ICollection CreateDataSource()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal)));
![](/Images/OutliningIndicators/InBlock.gif)
for (int i = 0; i < 40; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Random rd = new System.Random(Environment.TickCount * i); ;
dr = dt.NewRow();
dr[0] = "班级" + i.ToString();
dr[1] = "学生" + i.ToString();
dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[3] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[4] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
dt.Rows.Add(dr);
}
System.Data.DataView dv = new System.Data.DataView(dt);
return dv;
}
![](/Images/OutliningIndicators/InBlock.gif)
protected void Page_Load(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!IsPostBack)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
GridView1.BorderColor = System.Drawing.Color.DarkOrange;
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
}
![](/Images/OutliningIndicators/InBlock.gif)
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if (e.Row.RowType == DataControlRowType.Header)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//创建一个GridViewRow,相当于表格的 TR 一行
GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
string HeaderBackColor = "#EDEDED";
rowHeader.BackColor = System.Drawing.ColorTranslator.FromHtml(HeaderBackColor);
string thstyle = "style='position: relative; top:expression(this.offsetParent.scrollTop - 1); table-layout:fixed;z-index: 10;'";
![](/Images/OutliningIndicators/InBlock.gif)
Literal newCells = new Literal();
newCells.Text = @"表头文字1</th>
<th " + thstyle + @" colspan='2'>表头文字2</th>
<th " + thstyle + @" colspan='2'>表头文字3</th>
<th " + thstyle + @">表头文字4</th>
</tr>
<tr bgcolor='" + HeaderBackColor + "'>";
newCells.Text += @"
<th " + thstyle + @" colspan='2'>表头文字5</th>
<th " + thstyle + @" rowspan='2'>表头文字6</th>
<th " + thstyle + @" colspan='2'>表头文字7</th>
</tr>
<tr bgcolor='" + HeaderBackColor + "'>";
newCells.Text += @"
<th " + thstyle + @">表头文字8</th>
<th " + thstyle + @">表头文字9</th>
<th " + thstyle + @">表头文字10</th>
<th " + thstyle + @">表头文字11</th>
<th " + thstyle + @">表头文字12";
![](/Images/OutliningIndicators/InBlock.gif)
TableCellCollection cells = e.Row.Cells;
TableHeaderCell headerCell = new TableHeaderCell();
//下面的属性设置与 <td rowspan='2'>关键单元格</td> 要一致
headerCell.RowSpan = 2;
headerCell.Controls.Add(newCells);
headerCell.Attributes.Add("style", "position: relative; top:expression(this.offsetParent.scrollTop - 1); table-layout:fixed;z-index: 10;");
rowHeader.Cells.Add(headerCell);
![](/Images/OutliningIndicators/InBlock.gif)
rowHeader.Cells.Add(headerCell);
rowHeader.Visible = true;
//添加到 GridView1
GridView1.Controls[0].Controls.AddAt(0, rowHeader);
![](/Images/OutliningIndicators/InBlock.gif)
//TableCellCollection cells = e.Row.Cells;
//TableCell tc = new TableCell();
//tc.Text = newCells.Text;
//tc.Attributes.Add("style", "position: relative; top: table-layout:fixed;expression(this.offsetParent.scrollTop);z-index: 10;");
//rowHeader.Cells.Add(tc);
//GridView1.Controls[0].Controls.AddAt(0, rowHeader);
![](/Images/OutliningIndicators/InBlock.gif)
//TableCellCollection tcl = e.Row.Cells;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////清除自动生成的表头
//tcl.Clear();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////添加新的表头
//tcl.Add(new TableHeaderCell());
//tcl[0].RowSpan = 2;
//tcl[0].Text = "标题s";
//Label l1 = new Label();
//l1.Text = "uu";
//Button bt = new Button();
//bt.Text = "1";
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////因为自定义表头,所以原来系统中实现的排序功能就失效了
////下面语句可以自己在表头中添加控件,自己实现排序功能
//tcl[0].Controls.Add(l1);
//tcl[0].Controls.Add(bt);
![](/Images/OutliningIndicators/InBlock.gif)
//tcl.Add(new TableHeaderCell());
//tcl[1].ColumnSpan = 2;
//tcl[1].Text = "标题2";
![](/Images/OutliningIndicators/InBlock.gif)
//tcl.Add(new TableHeaderCell());
//tcl[2].RowSpan = 2;
//tcl[2].Text = "标题3";
![](/Images/OutliningIndicators/InBlock.gif)
//tcl.Add(new TableHeaderCell());
//tcl[3].ColumnSpan = 3;
//tcl[3].Text = "标题4";
![](/Images/OutliningIndicators/InBlock.gif)
//tcl.Add(new TableHeaderCell());
//tcl[4].RowSpan = 2;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////这段是重点 其实在生成的html中tcl[4]转化为<th>标题5</th>
////所以依照该原则注入html标签来实现,原理有些类似于SQL注入攻击
//tcl[4].Text = "标题5</th></tr><tr><th>标题2-1</th><th>标题2-2</th><th>标题4-1</th><th>标题4-2</th><th>标题4-3";
![](/Images/OutliningIndicators/InBlock.gif)
}
}
![](/Images/OutliningIndicators/InBlock.gif)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (e.Row.RowType == DataControlRowType.Header)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
e.Row.Attributes.Add("style", "background:#9999FF;color:#FFFFFF;font-size:14px;CellSpacing:0;");
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
e.Row.Attributes.Add("style", "background:#FFF;");
![](/Images/OutliningIndicators/InBlock.gif)
}
}
}
![](/Images/OutliningIndicators/None.gif)