Table控件提供了一种更加灵活的为各项数据以及动态生成的控件定位的方式
动态地创建一个表包含三个步骤。首先,创建 TableCell 对象,表示行中的单元格。添加单元格的内容有两种方法:设置 Text 属性,或者向 TableCell 的 Control.Controls 集合添加控件。接下来,创建 TableRow 以表示表中的行。将此前创建的 TableCell 对象添加到 TableRow 的 Cells 集合中。最后,将 TableRow 添加到 Table 控件的 Rows 集合中。对表中的各行重复此过程。
实例:从数据库表中取得产品大类和小类,灵活显示产品类别及其之间的关系列表
1
2 //产品大类数
3 int ParentCount=dt.Rows.Count;
4 //产品大类的计数器
5 int ParentCounter=ParentCount;
6 // 总行数
7 int rowCnt=0;
8 //满行数
9 int tempn=ParentCount/3;
10 if(3*tempn<ParentCount)
11 rowCnt=ParentCount/3+1;
12 else
13 rowCnt=tempn;
14
15 // 当前行
16 int rowCtr;
17 // 当前单元格
18 int cellCtr;
19 // 每行的列数
20 int cellCnt=3;
21
22
23 for(rowCtr=1; rowCtr <= rowCnt; rowCtr++)
24 {
25 // Create new row and add it to the table.
26 TableRow tRow = new TableRow();
27 Table1.Rows.Add(tRow);
28 for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
29 {
30 // Create a new cell and add it to the row.
31 TableCell tCell = new TableCell();
32 tRow.Cells.Add(tCell);
33 //string prodID = rowCtr + "_" + cellCtr;
34 //产品名称
35 string prodName=dt.Rows[ParentCounter-1]["Name"].ToString();
36 // Add a literal text as control.
37 tCell.Controls.Add(new LiteralControl(" "));
38 // Create Hyperlink Web Server control and add to cell
39 System.Web.UI.WebControls.HyperLink h = new HyperLink();
40
41 DataTable dt1=new DataTable();
42 dt1=pc.GetCategoriesByParentID(dt.Rows[ParentCounter-1]["ID"].ToString());
43
44 //小类数量
45 int ChildCount=dt1.Rows.Count;
46 h.Text = "<STRONG>"+prodName+"("+ChildCount.ToString()+")<STRONG><br>";
47 h.NavigateUrl = "product/product_list.aspx?id="+dt.Rows[ParentCounter-1]["ID"].ToString();
48
49 tCell.Controls.Add(h);
50 //增加小类
51
52 if(dt1.Rows.Count>0)
53 {
54 if(dt1.Rows.Count==1)
55 {
56 System.Web.UI.WebControls.HyperLink h1 = new HyperLink();
57 h1.Text=dt1.Rows[0]["Name"].ToString();
58 h1.NavigateUrl = "product/product_list.aspx?id="+dt1.Rows[0]["ID"].ToString();
59 tCell.Controls.Add(h1);
60 }
61
62 if(dt1.Rows.Count==2)
63 {
64 System.Web.UI.WebControls.HyperLink h1 = new HyperLink();
65 h1.Text=dt1.Rows[0]["Name"].ToString()+"、";
66 h1.NavigateUrl ="product/product_list.aspx?id="+dt1.Rows[0]["ID"].ToString();
67 tCell.Controls.Add(h1);
68 System.Web.UI.WebControls.HyperLink h2 = new HyperLink();
69 h2.Text=dt1.Rows[1]["Name"].ToString();
70 h2.NavigateUrl = "product/product_list.aspx?id="+dt1.Rows[1]["ID"].ToString();
71 tCell.Controls.Add(h2);
72 }
73 if(dt1.Rows.Count>2)
74 {
75 System.Web.UI.WebControls.HyperLink h1 = new HyperLink();
76 h1.Text=dt1.Rows[0]["Name"].ToString()+"、";
77 h1.NavigateUrl ="product/product_list.aspx?id="+dt1.Rows[0]["ID"].ToString();
78 tCell.Controls.Add(h1);
79 System.Web.UI.WebControls.HyperLink h2 = new HyperLink();
80 h2.Text=dt1.Rows[1]["Name"].ToString()+"";
81 h2.NavigateUrl = "product/product_list.aspx?id="+dt1.Rows[1]["ID"].ToString();
82 tCell.Controls.Add(h2);
83 }
84 }
85
86
87 ParentCounter--;
88 if(ParentCounter<0)
89 return ;
90 }
91 }
实例:从数据库表中取得产品大类和小类,灵活显示产品类别及其之间的关系列表
1
2 //产品大类数
3 int ParentCount=dt.Rows.Count;
4 //产品大类的计数器
5 int ParentCounter=ParentCount;
6 // 总行数
7 int rowCnt=0;
8 //满行数
9 int tempn=ParentCount/3;
10 if(3*tempn<ParentCount)
11 rowCnt=ParentCount/3+1;
12 else
13 rowCnt=tempn;
14
15 // 当前行
16 int rowCtr;
17 // 当前单元格
18 int cellCtr;
19 // 每行的列数
20 int cellCnt=3;
21
22
23 for(rowCtr=1; rowCtr <= rowCnt; rowCtr++)
24 {
25 // Create new row and add it to the table.
26 TableRow tRow = new TableRow();
27 Table1.Rows.Add(tRow);
28 for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
29 {
30 // Create a new cell and add it to the row.
31 TableCell tCell = new TableCell();
32 tRow.Cells.Add(tCell);
33 //string prodID = rowCtr + "_" + cellCtr;
34 //产品名称
35 string prodName=dt.Rows[ParentCounter-1]["Name"].ToString();
36 // Add a literal text as control.
37 tCell.Controls.Add(new LiteralControl(" "));
38 // Create Hyperlink Web Server control and add to cell
39 System.Web.UI.WebControls.HyperLink h = new HyperLink();
40
41 DataTable dt1=new DataTable();
42 dt1=pc.GetCategoriesByParentID(dt.Rows[ParentCounter-1]["ID"].ToString());
43
44 //小类数量
45 int ChildCount=dt1.Rows.Count;
46 h.Text = "<STRONG>"+prodName+"("+ChildCount.ToString()+")<STRONG><br>";
47 h.NavigateUrl = "product/product_list.aspx?id="+dt.Rows[ParentCounter-1]["ID"].ToString();
48
49 tCell.Controls.Add(h);
50 //增加小类
51
52 if(dt1.Rows.Count>0)
53 {
54 if(dt1.Rows.Count==1)
55 {
56 System.Web.UI.WebControls.HyperLink h1 = new HyperLink();
57 h1.Text=dt1.Rows[0]["Name"].ToString();
58 h1.NavigateUrl = "product/product_list.aspx?id="+dt1.Rows[0]["ID"].ToString();
59 tCell.Controls.Add(h1);
60 }
61
62 if(dt1.Rows.Count==2)
63 {
64 System.Web.UI.WebControls.HyperLink h1 = new HyperLink();
65 h1.Text=dt1.Rows[0]["Name"].ToString()+"、";
66 h1.NavigateUrl ="product/product_list.aspx?id="+dt1.Rows[0]["ID"].ToString();
67 tCell.Controls.Add(h1);
68 System.Web.UI.WebControls.HyperLink h2 = new HyperLink();
69 h2.Text=dt1.Rows[1]["Name"].ToString();
70 h2.NavigateUrl = "product/product_list.aspx?id="+dt1.Rows[1]["ID"].ToString();
71 tCell.Controls.Add(h2);
72 }
73 if(dt1.Rows.Count>2)
74 {
75 System.Web.UI.WebControls.HyperLink h1 = new HyperLink();
76 h1.Text=dt1.Rows[0]["Name"].ToString()+"、";
77 h1.NavigateUrl ="product/product_list.aspx?id="+dt1.Rows[0]["ID"].ToString();
78 tCell.Controls.Add(h1);
79 System.Web.UI.WebControls.HyperLink h2 = new HyperLink();
80 h2.Text=dt1.Rows[1]["Name"].ToString()+"";
81 h2.NavigateUrl = "product/product_list.aspx?id="+dt1.Rows[1]["ID"].ToString();
82 tCell.Controls.Add(h2);
83 }
84 }
85
86
87 ParentCounter--;
88 if(ParentCounter<0)
89 return ;
90 }
91 }