In developing ASP.NET site projects, data is often displayed in tabular form. The most commonly used method is probably the way DataGrid binds DataSet data. In the software projects I have worked on, there are three typical ways of processing tabular data.
1. DataGrid binds data source. This method is used the most, but the perfect binding method between DataGrid and ADO.NET is still annoying. The uniform DataGrid style is difficult to adapt to the special styles of different projects, and personalization on the DataGrid will be very troublesome.
2. Use XML (data) + XSL (style sheet). As you can understand, the implementation mechanism of binding DataSet to DataGrid is nothing more than this. As shown in the figure, to implement such a table, developers can design the XSL style as much as they like.
3. Draw data directly to HTML. This method is a bit crude, but sometimes it is very effective. Let’s look at the implementation code first. The following code implements the table shown in the above figure.
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="宋体">General Management Department Personnel Positions</FONT></STRONG></FONT></P>
9 </td>
10 </tr>
11 <tr>
12 <td align="center" bgcolor="#003399"><FONT size="2" color="#ffffff"><STRONG>Personnel name</STRONG></FONT></td>
13 <td align="center" bgcolor="#003399"><FONT size="2" color="#ffffff"><STRONG>Department Position</STRONG></FONT></td>
14 </tr>
15 </TABLE>
16 </td>
17 </tr>
18 </table>
Directly use the Add method of ASP.NET WebControls to add the Label to the HTML Cell.
1public class WebForm2 : System.Web.UI.Page
2 {
3
4 struct PersonRole
5 {
6 public string name;
7 public string role;
8}
9
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 {
14 //Put user code here to initialize the page
15 if(Request.QueryString["AuditItemID"] != null)
16 {
17 strAuditItemID = Request.QueryString["AuditItemID"].ToString();
18}
19
20 GetTeamMember(strAuditItemID);
twenty one }
twenty two
23 Code generated by the Web Forms Designer#region Code generated by the Web Forms Designer
24 override protected void OnInit(EventArgs e)
25 {
26 //
27 // CODEGEN: This call is required by the ASP.NET Web Forms designer.
28 //
29 InitializeComponent();
30 base.OnInit(e);
31}
32
33 /**//// <summary>
34 /// Designer supports required methods - do not use code editor to modify
35 /// The content of this method.
36 /// </summary>
37 private void InitializeComponent()
38 {
39 this.Load += new System.EventHandler(this.Page_Load);
40
41 }
42 #endregion
43
44 private void GetTeamMember(string AuditItemID)
45 {
46 string strMaster, strTeamLeader, strPM;
47 ArrayList al = GetTeamMemberName(AuditItemID, out strMaster, out strTeamLeader, out strPM);
48
49 foreach(PersonRole pr in al)
50 {
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
57 cell.Controls.Add(lbl);
58 HtmlTableRow row=new HtmlTableRow();
59 row.Cells.Add(cell);
60
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
67 cellRole.Controls.Add(lblRole);
68 row.Cells.Add(cellRole);
69
70 tblContainer.Rows.Add(row);
71 }
72 }
73
74 private ArrayList GetTeamMemberName(string AuditItemID, out string strMasterName, out string strTeamLeader,out string strPM)
75 {
76 ArrayList al = new ArrayList();
77 strMasterName = "None";
78 strTeamLeader = "None";
79 strPM = "None";
80
81 PersonRole pr;
82 pr.name = "Zhang San";
83 pr.role = "General Manager";
84 al.Add(pr);
85
86 pr.name = "李思";
87 pr.role = "Deputy General Manager";
88 al.Add(pr);
89
90 pr.name = "王五";
91 pr.role = "section member";
92 al.Add(pr);
93
94 pr.name = "Zhao Liu";
95 pr.role = "section member";
96 al.Add(pr);
97
98 return al;
99 }
100}
When drawing a page, which method should be used is a matter of opinion and wisdom. In website development, these three methods have typical applications, especially the third method. I found that it is very effective in solving the performance problems of some page processing. Moreover, the layout page control can be automated, and personalized processing is easier than rewriting Render in DataGrid.