在开发ASP.NET站点项目中,经常用表格化的方式显示数据。最常用的可能就是DataGrid绑定DataSet数据的方式。在做过的软件项目中,有3种表格化数据的典型处理方式。
1、DataGrid绑定数据源。这种方式大家用的最多,但是DataGrid与ADO.NET完美的绑定方式,还是让人不爽。清一色的DataGrid风格很难适应不同项目的特殊风格,而且在DataGrid上做出的个性化处理也会非常的麻烦。
2、用XML(数据)+XSL(样式单)。大家能理解,DataSet绑定到DataGrid的实现机理不过如此。如图所示,实现这样的一个表格,开发人员可以尽情的设计XSL的风格样式。
3、直接将数据绘制到HTML。这个方式有些土,但有些时候却非常有效,先看实现的代码。下述代码是实现上图所示的表格。
直接用ASP.NET WebControls的Add方法,将Label添加到HTML的Cell中。
1、DataGrid绑定数据源。这种方式大家用的最多,但是DataGrid与ADO.NET完美的绑定方式,还是让人不爽。清一色的DataGrid风格很难适应不同项目的特殊风格,而且在DataGrid上做出的个性化处理也会非常的麻烦。
2、用XML(数据)+XSL(样式单)。大家能理解,DataSet绑定到DataGrid的实现机理不过如此。如图所示,实现这样的一个表格,开发人员可以尽情的设计XSL的风格样式。
![](http://dev.yesky.com/imagelist/06/01/20r54t9zsv7g.gif)
3、直接将数据绘制到HTML。这个方式有些土,但有些时候却非常有效,先看实现的代码。下述代码是实现上图所示的表格。
1
<
table
style
="WIDTH: 100%; BORDER-COLLAPSE: collapse; HEIGHT: 10px"
>
2
<
tr
>
3
<
td
align
="center"
>
4
<
TABLE
id
="tblContainer"
class
="MsoNormalTable"
style
="WIDTH: 380px; BORDER-COLLAPSE: collapse; HEIGHT: 10px"
5
cellSpacing
="0"
cellPadding
="6"
border
="1"
runat
="server"
bordercolor
="#99cccc"
>
6
<
tr
>
7
<
td
colspan
="2"
align
="center"
>
8
<
P
><
FONT
size
="3"
><
STRONG
><
FONT
face
="宋体"
>
综合管理部人员职务
</
FONT
></
STRONG
></
FONT
></
P
>
9
</
td
>
10
</
tr
>
11
<
tr
>
12
<
td
align
="center"
bgcolor
="#003399"
><
FONT
size
="2"
color
="#ffffff"
><
STRONG
>
人员姓名
</
STRONG
></
FONT
></
td
>
13
<
td
align
="center"
bgcolor
="#003399"
><
FONT
size
="2"
color
="#ffffff"
><
STRONG
>
部门职务
</
STRONG
></
FONT
></
td
>
14
</
tr
>
15
</
TABLE
>
16
</
td
>
17
</
tr
>
18
</
table
>
![](http://dev.yesky.com/imagelist/06/01/kn4u5gmyzafo.gif)
2
![](http://dev.yesky.com/imagelist/06/01/436yc3yo2q44.gif)
3
![](http://dev.yesky.com/imagelist/06/01/v2306e03oo8e.gif)
4
![](http://dev.yesky.com/imagelist/06/01/qn3s69d3okez.gif)
5
![](http://dev.yesky.com/imagelist/06/01/j4y5hw0e5w7x.gif)
6
![](http://dev.yesky.com/imagelist/06/01/lv0368910va2.gif)
7
![](http://dev.yesky.com/imagelist/06/01/dbn64917rl45.gif)
8
![](http://dev.yesky.com/imagelist/06/01/c111ipr59y3x.gif)
9
![](http://dev.yesky.com/imagelist/06/01/qs6o9e5eakn9.gif)
10
![](http://dev.yesky.com/imagelist/06/01/6e0546vl03rs.gif)
11
![](http://dev.yesky.com/imagelist/06/01/dlvsf8wozc5l.gif)
12
![](http://dev.yesky.com/imagelist/06/01/w3501vk3687v.gif)
13
![](http://dev.yesky.com/imagelist/06/01/5f8pk7y1lr8c.gif)
14
![](http://dev.yesky.com/imagelist/06/01/u05bs1qkwig4.gif)
15
![](http://dev.yesky.com/imagelist/06/01/379a1lqba23s.gif)
16
![](http://dev.yesky.com/imagelist/06/01/yx67c77j4eu1.gif)
17
![](http://dev.yesky.com/imagelist/06/01/374mqv1e05f1.gif)
18
![](http://dev.yesky.com/imagelist/06/01/f1l7jxt13162.gif)
直接用ASP.NET WebControls的Add方法,将Label添加到HTML的Cell中。
1
public
class
WebForm2 : System.Web.UI.Page
2![](http://dev.yesky.com/imagelist/06/01/2tu94z976dn5.gif)
{
3
4
struct PersonRole
5![](http://dev.yesky.com/imagelist/06/01/1955o46u8202.gif)
{
6
public string name;
7
public string role;
8
}
9![](http://dev.yesky.com/imagelist/06/01/a07325of20fx.gif)
10
protected System.Web.UI.HtmlControls.HtmlTable tblContainer;
11
public string strAuditItemID = "A899B637-AC47-42EB-9B61-A61C9C880DDC";
12
private void Page_Load(object sender, System.EventArgs e)
13![](http://dev.yesky.com/imagelist/06/01/0gfnap0dpu63.gif)
{
14
// 在此处放置用户代码以初始化页面
15
if(Request.QueryString["AuditItemID"] != null)
16![](http://dev.yesky.com/imagelist/06/01/118a91801aq2.gif)
{
17
strAuditItemID = Request.QueryString["AuditItemID"].ToString();
18
}
19![](http://dev.yesky.com/imagelist/06/01/ugvd6hu85sus.gif)
20
GetTeamMember(strAuditItemID);
21
}
22![](http://dev.yesky.com/imagelist/06/01/s8q75e7aw415.gif)
23![](http://dev.yesky.com/imagelist/06/01/pju092884a96.gif)
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
24
override protected void OnInit(EventArgs e)
25![](http://dev.yesky.com/imagelist/06/01/tc71rx4j5mhy.gif)
{
26
//
27
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
28
//
29
InitializeComponent();
30
base.OnInit(e);
31
}
32
33![](http://dev.yesky.com/imagelist/06/01/8wt77e03cxy6.gif)
/**//// <summary>
34
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
35
/// 此方法的内容。
36
/// </summary>
37
private void InitializeComponent()
38![](http://dev.yesky.com/imagelist/06/01/988fv9bto36g.gif)
{
39
this.Load += new System.EventHandler(this.Page_Load);
40![](http://dev.yesky.com/imagelist/06/01/8cc7h9pipiz1.gif)
41
}
42
#endregion
43![](http://dev.yesky.com/imagelist/06/01/2ms919c2wbvz.gif)
44
private void GetTeamMember(string AuditItemID)
45![](http://dev.yesky.com/imagelist/06/01/jfj4a927lc32.gif)
{
46
string strMaster, strTeamLeader, strPM;
47
ArrayList al = GetTeamMemberName(AuditItemID, out strMaster, out strTeamLeader, out strPM);
48![](http://dev.yesky.com/imagelist/06/01/4sarvd0zi227.gif)
49
foreach(PersonRole pr in al)
50![](http://dev.yesky.com/imagelist/06/01/mp31by56356k.gif)
{
51
HtmlTableCell cell=new HtmlTableCell();
52
cell.Align = "Center";
53
Label lbl = new Label();
54
lbl.Text = pr.name;
55
lbl.Font.Size = 9;
56![](http://dev.yesky.com/imagelist/06/01/6lqm5ibq4c4c.gif)
57
cell.Controls.Add(lbl);
58
HtmlTableRow row=new HtmlTableRow();
59
row.Cells.Add(cell);
60![](http://dev.yesky.com/imagelist/06/01/ihl12d5k6sgj.gif)
61
HtmlTableCell cellRole = new HtmlTableCell();
62
cellRole.Align = "Center";
63
Label lblRole = new Label();
64
lblRole.Text = pr.role;
65
lblRole.Font.Size = 9;
66![](http://dev.yesky.com/imagelist/06/01/0r2v95w2r544.gif)
67
cellRole.Controls.Add(lblRole);
68
row.Cells.Add(cellRole);
69![](http://dev.yesky.com/imagelist/06/01/3lk8bb1pnli4.gif)
70
tblContainer.Rows.Add(row);
71
}
72
}
73![](http://dev.yesky.com/imagelist/06/01/lo47bogm3c6a.gif)
74
private ArrayList GetTeamMemberName(string AuditItemID, out string strMasterName, out string strTeamLeader,out string strPM)
75![](http://dev.yesky.com/imagelist/06/01/147m8c765fwj.gif)
{
76
ArrayList al = new ArrayList();
77
strMasterName = "无";
78
strTeamLeader = "无";
79
strPM = "无";
80
81
PersonRole pr;
82
pr.name = "张三";
83
pr.role = "总经理";
84
al.Add(pr);
85
86
pr.name = "李四";
87
pr.role = "副总经理";
88
al.Add(pr);
89![](http://dev.yesky.com/imagelist/06/01/ue52f3yj47eh.gif)
90
pr.name = "王五";
91
pr.role = "科员";
92
al.Add(pr);
93![](http://dev.yesky.com/imagelist/06/01/tgj9w3rs083z.gif)
94
pr.name = "赵六";
95
pr.role = "科员";
96
al.Add(pr);
97![](http://dev.yesky.com/imagelist/06/01/1e5u973bd4k2.gif)
98
return al;
99
}
100
}
![](http://dev.yesky.com/imagelist/06/01/55j1m395e1yb.gif)
2
![](http://dev.yesky.com/imagelist/06/01/2tu94z976dn5.gif)
![](http://dev.yesky.com/imagelist/06/01/h3k42eld4v1y.gif)
![](http://dev.yesky.com/imagelist/06/01/owx4x092l9au.gif)
3
![](http://dev.yesky.com/imagelist/06/01/4az0za6ie8j1.gif)
4
![](http://dev.yesky.com/imagelist/06/01/w2q2513obg58.gif)
5
![](http://dev.yesky.com/imagelist/06/01/1955o46u8202.gif)
![](http://dev.yesky.com/imagelist/06/01/o520p678kipm.gif)
![](http://dev.yesky.com/imagelist/06/01/pb9p5440cv8a.gif)
6
![](http://dev.yesky.com/imagelist/06/01/si92eda53h3v.gif)
7
![](http://dev.yesky.com/imagelist/06/01/71id198d26uz.gif)
8
![](http://dev.yesky.com/imagelist/06/01/kv07shg44718.gif)
9
![](http://dev.yesky.com/imagelist/06/01/a07325of20fx.gif)
10
![](http://dev.yesky.com/imagelist/06/01/dd6q7ljojeut.gif)
11
![](http://dev.yesky.com/imagelist/06/01/65ffe0ajr411.gif)
12
![](http://dev.yesky.com/imagelist/06/01/8s09h9by49vr.gif)
13
![](http://dev.yesky.com/imagelist/06/01/0gfnap0dpu63.gif)
![](http://dev.yesky.com/imagelist/06/01/by485kj7a08y.gif)
![](http://dev.yesky.com/imagelist/06/01/5a8qbu6i6551.gif)
14
![](http://dev.yesky.com/imagelist/06/01/3vvj65lnaf7y.gif)
15
![](http://dev.yesky.com/imagelist/06/01/sgqyyypr5jka.gif)
16
![](http://dev.yesky.com/imagelist/06/01/118a91801aq2.gif)
![](http://dev.yesky.com/imagelist/06/01/3kegr943623y.gif)
![](http://dev.yesky.com/imagelist/06/01/hq8uantzy924.gif)
17
![](http://dev.yesky.com/imagelist/06/01/95evyxnx4p3t.gif)
18
![](http://dev.yesky.com/imagelist/06/01/6lxhlue22n6n.gif)
19
![](http://dev.yesky.com/imagelist/06/01/ugvd6hu85sus.gif)
20
![](http://dev.yesky.com/imagelist/06/01/w7zzghc0253k.gif)
21
![](http://dev.yesky.com/imagelist/06/01/6e9l3b1i0uy9.gif)
22
![](http://dev.yesky.com/imagelist/06/01/s8q75e7aw415.gif)
23
![](http://dev.yesky.com/imagelist/06/01/pju092884a96.gif)
![](http://dev.yesky.com/imagelist/06/01/9ub3y535sh1k.gif)
24
![](http://dev.yesky.com/imagelist/06/01/j15o757bo64k.gif)
25
![](http://dev.yesky.com/imagelist/06/01/tc71rx4j5mhy.gif)
![](http://dev.yesky.com/imagelist/06/01/15633dzfm533.gif)
![](http://dev.yesky.com/imagelist/06/01/c1hoabh7a81m.gif)
26
![](http://dev.yesky.com/imagelist/06/01/vs53sa3ije51.gif)
27
![](http://dev.yesky.com/imagelist/06/01/78ckix6bjgp6.gif)
28
![](http://dev.yesky.com/imagelist/06/01/11fm8z3394c8.gif)
29
![](http://dev.yesky.com/imagelist/06/01/e5aa81109966.gif)
30
![](http://dev.yesky.com/imagelist/06/01/boy5v3q49pmf.gif)
31
![](http://dev.yesky.com/imagelist/06/01/mfudx4x5rhq2.gif)
32
![](http://dev.yesky.com/imagelist/06/01/9tqz78320lb2.gif)
33
![](http://dev.yesky.com/imagelist/06/01/8wt77e03cxy6.gif)
![](http://dev.yesky.com/imagelist/06/01/i29skayttyt9.gif)
34
![](http://dev.yesky.com/imagelist/06/01/7zb9q4vhl9i8.gif)
35
![](http://dev.yesky.com/imagelist/06/01/uqjge92g6s60.gif)
36
![](http://dev.yesky.com/imagelist/06/01/5qjb2biv393o.gif)
37
![](http://dev.yesky.com/imagelist/06/01/c2ozam2vv7oo.gif)
38
![](http://dev.yesky.com/imagelist/06/01/988fv9bto36g.gif)
![](http://dev.yesky.com/imagelist/06/01/4187a4817354.gif)
![](http://dev.yesky.com/imagelist/06/01/6kp4l428ipjj.gif)
39
![](http://dev.yesky.com/imagelist/06/01/eq7tgwo2f697.gif)
40
![](http://dev.yesky.com/imagelist/06/01/8cc7h9pipiz1.gif)
41
![](http://dev.yesky.com/imagelist/06/01/78a6793236pm.gif)
42
![](http://dev.yesky.com/imagelist/06/01/v1y6ncy218t1.gif)
43
![](http://dev.yesky.com/imagelist/06/01/2ms919c2wbvz.gif)
44
![](http://dev.yesky.com/imagelist/06/01/5gejf2lco6ts.gif)
45
![](http://dev.yesky.com/imagelist/06/01/jfj4a927lc32.gif)
![](http://dev.yesky.com/imagelist/06/01/8uk65prtqqvs.gif)
![](http://dev.yesky.com/imagelist/06/01/m34jjkjssu5d.gif)
46
![](http://dev.yesky.com/imagelist/06/01/65o88zfw23p2.gif)
47
![](http://dev.yesky.com/imagelist/06/01/m0ae7xijxjqu.gif)
48
![](http://dev.yesky.com/imagelist/06/01/4sarvd0zi227.gif)
49
![](http://dev.yesky.com/imagelist/06/01/ui619gysc44q.gif)
50
![](http://dev.yesky.com/imagelist/06/01/mp31by56356k.gif)
![](http://dev.yesky.com/imagelist/06/01/853b2zjl7k1g.gif)
![](http://dev.yesky.com/imagelist/06/01/001rxxe8fd78.gif)
51
![](http://dev.yesky.com/imagelist/06/01/s5pvj49ufw07.gif)
52
![](http://dev.yesky.com/imagelist/06/01/uv8vlm007ir5.gif)
53
![](http://dev.yesky.com/imagelist/06/01/1yi827x0517h.gif)
54
![](http://dev.yesky.com/imagelist/06/01/0j3l5ax6n32w.gif)
55
![](http://dev.yesky.com/imagelist/06/01/bx7329e80mvy.gif)
56
![](http://dev.yesky.com/imagelist/06/01/6lqm5ibq4c4c.gif)
57
![](http://dev.yesky.com/imagelist/06/01/rs9804tibgt6.gif)
58
![](http://dev.yesky.com/imagelist/06/01/pskz077sp23r.gif)
59
![](http://dev.yesky.com/imagelist/06/01/5hzj39336ncp.gif)
60
![](http://dev.yesky.com/imagelist/06/01/ihl12d5k6sgj.gif)
61
![](http://dev.yesky.com/imagelist/06/01/h59i0y827w28.gif)
62
![](http://dev.yesky.com/imagelist/06/01/c93b99wsisd1.gif)
63
![](http://dev.yesky.com/imagelist/06/01/94s0xf74cx31.gif)
64
![](http://dev.yesky.com/imagelist/06/01/04pxlty56th8.gif)
65
![](http://dev.yesky.com/imagelist/06/01/d5s35xk483du.gif)
66
![](http://dev.yesky.com/imagelist/06/01/0r2v95w2r544.gif)
67
![](http://dev.yesky.com/imagelist/06/01/12pq3z452wpf.gif)
68
![](http://dev.yesky.com/imagelist/06/01/n5891dp05ucc.gif)
69
![](http://dev.yesky.com/imagelist/06/01/3lk8bb1pnli4.gif)
70
![](http://dev.yesky.com/imagelist/06/01/ttwd736l38qv.gif)
71
![](http://dev.yesky.com/imagelist/06/01/ok3z96f0930a.gif)
72
![](http://dev.yesky.com/imagelist/06/01/h88l7g3261pt.gif)
73
![](http://dev.yesky.com/imagelist/06/01/lo47bogm3c6a.gif)
74
![](http://dev.yesky.com/imagelist/06/01/l456569f89q1.gif)
75
![](http://dev.yesky.com/imagelist/06/01/147m8c765fwj.gif)
![](http://dev.yesky.com/imagelist/06/01/q1vl3535p0qz.gif)
![](http://dev.yesky.com/imagelist/06/01/3934929ehpyn.gif)
76
![](http://dev.yesky.com/imagelist/06/01/u3s3ubidz564.gif)
77
![](http://dev.yesky.com/imagelist/06/01/83by840m4x9i.gif)
78
![](http://dev.yesky.com/imagelist/06/01/n14bh19yy47r.gif)
79
![](http://dev.yesky.com/imagelist/06/01/j0lou47l07ga.gif)
80
![](http://dev.yesky.com/imagelist/06/01/ubb7p6ff199s.gif)
81
![](http://dev.yesky.com/imagelist/06/01/6ri43cm41k66.gif)
82
![](http://dev.yesky.com/imagelist/06/01/yd67t13k9x16.gif)
83
![](http://dev.yesky.com/imagelist/06/01/49ea0evh1lk8.gif)
84
![](http://dev.yesky.com/imagelist/06/01/07ofijmk1t39.gif)
85
![](http://dev.yesky.com/imagelist/06/01/14513k4o2nyk.gif)
86
![](http://dev.yesky.com/imagelist/06/01/475i3793az05.gif)
87
![](http://dev.yesky.com/imagelist/06/01/yn41fcqc531n.gif)
88
![](http://dev.yesky.com/imagelist/06/01/73izp51vgpbr.gif)
89
![](http://dev.yesky.com/imagelist/06/01/ue52f3yj47eh.gif)
90
![](http://dev.yesky.com/imagelist/06/01/0u7iws9w53xx.gif)
91
![](http://dev.yesky.com/imagelist/06/01/l3ni1etr0051.gif)
92
![](http://dev.yesky.com/imagelist/06/01/ilyhchh761qm.gif)
93
![](http://dev.yesky.com/imagelist/06/01/tgj9w3rs083z.gif)
94
![](http://dev.yesky.com/imagelist/06/01/u22y9u9xiy60.gif)
95
![](http://dev.yesky.com/imagelist/06/01/t5750ncr7c29.gif)
96
![](http://dev.yesky.com/imagelist/06/01/pyxhh7cl67w6.gif)
97
![](http://dev.yesky.com/imagelist/06/01/1e5u973bd4k2.gif)
98
![](http://dev.yesky.com/imagelist/06/01/rzip2081o4ex.gif)
99
![](http://dev.yesky.com/imagelist/06/01/luyk1124q993.gif)
100
![](http://dev.yesky.com/imagelist/06/01/jql0535lv5s3.gif)
在绘制页面的时候,用哪种方式应该是仁者见仁、智者见智。在站点开发中,这3种方式都有典型的应用,特别是第三种,我发现在解决部分页面处理的性能问题中应用的非常有效。况且可以自动化的Layout页面控件,个性化处理比DataGrid中重写Render更加容易。