動態產生asp:Table控件,並加入PlaceHolder容器中,然后把List<T>數據綁定至asp:Table控件中顯示。首先創建一個對象,這是個紙張對象。有ID和Size兩個attribute。 在對象中,還產生一個Collection集合,存儲紙張類型。

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Pager
/// </summary>
namespace Insus.NET
{
public class Pager
{
private int _ID;
private string _Size;
public int ID
{
get { return _ID; }
set { _ID = value; }
}
public string Size
{
get { return _Size; }
set { _Size = value; }
}
public Pager()
{
//
// TODO: Add constructor logic here
//
}
public Pager( int id, string size)
{
this._ID = id;
this._Size = size;
}
public List<Pager> PagerCollections()
{
List<Pager> p = new List<Pager>();
p.Add( new Pager( 1, " A0 "));
p.Add( new Pager( 2, " A1 "));
p.Add( new Pager( 3, " A2 "));
p.Add( new Pager( 4, " A3 "));
p.Add( new Pager( 5, " A4 "));
p.Add( new Pager( 6, " A5 "));
return p;
}
}
}
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Pager
/// </summary>
namespace Insus.NET
{
public class Pager
{
private int _ID;
private string _Size;
public int ID
{
get { return _ID; }
set { _ID = value; }
}
public string Size
{
get { return _Size; }
set { _Size = value; }
}
public Pager()
{
//
// TODO: Add constructor logic here
//
}
public Pager( int id, string size)
{
this._ID = id;
this._Size = size;
}
public List<Pager> PagerCollections()
{
List<Pager> p = new List<Pager>();
p.Add( new Pager( 1, " A0 "));
p.Add( new Pager( 2, " A1 "));
p.Add( new Pager( 3, " A2 "));
p.Add( new Pager( 4, " A3 "));
p.Add( new Pager( 5, " A4 "));
p.Add( new Pager( 6, " A5 "));
return p;
}
}
}
有了數據源之后,就可以動態創建asp:Table了,內有注釋。

private
void DynamicCreateTable()
{
// 把PlaceHolder容器清除所有控件。
PlaceHolder1.Controls.Clear();
// 創建一個空asp:Table
Table oTable = new Table();
// 此表的寬度為百分之40
oTable.Width = Unit.Percentage( 40);
// 把這個asp:table加入PlaceHolder容器中。
PlaceHolder1.Controls.Add(oTable);
// 創建一行
TableRow tr = new TableRow();
// 指定行的ID。
tr.ID = " tr0 ";
// 創建第一行第一列,標題列
TableHeaderCell hc0 = new TableHeaderCell();
// 列顯示文本
hc0.Text = " ID ";
// 列寬度
hc0.BorderWidth = Unit.Pixel( 1);
// 列加入行中。
tr.Cells.Add(hc0);
TableHeaderCell hc1 = new TableHeaderCell();
hc1.Text = " Size ";
hc1.BorderWidth = Unit.Pixel( 1);
tr.Cells.Add(hc1);
// 把行加入表中。
oTable.Rows.Add(tr);
// for each List<T>
Pager objPg = new Pager();
objPg.PagerCollections().ForEach( delegate(Pager p)
{
TableRow tbr = new TableRow();
tbr.ID = " tr " + p.ID.ToString();
TableCell tc0 = new TableCell();
tc0.Text = p.ID.ToString();
tc0.BorderWidth = Unit.Pixel( 1);
tbr.Cells.Add(tc0);
TableCell tc1 = new TableCell();
tc1.Text = p.Size;
tc1.BorderWidth = Unit.Pixel( 1);
tbr.Cells.Add(tc1);
oTable.Rows.Add(tbr);
});
}
{
// 把PlaceHolder容器清除所有控件。
PlaceHolder1.Controls.Clear();
// 創建一個空asp:Table
Table oTable = new Table();
// 此表的寬度為百分之40
oTable.Width = Unit.Percentage( 40);
// 把這個asp:table加入PlaceHolder容器中。
PlaceHolder1.Controls.Add(oTable);
// 創建一行
TableRow tr = new TableRow();
// 指定行的ID。
tr.ID = " tr0 ";
// 創建第一行第一列,標題列
TableHeaderCell hc0 = new TableHeaderCell();
// 列顯示文本
hc0.Text = " ID ";
// 列寬度
hc0.BorderWidth = Unit.Pixel( 1);
// 列加入行中。
tr.Cells.Add(hc0);
TableHeaderCell hc1 = new TableHeaderCell();
hc1.Text = " Size ";
hc1.BorderWidth = Unit.Pixel( 1);
tr.Cells.Add(hc1);
// 把行加入表中。
oTable.Rows.Add(tr);
// for each List<T>
Pager objPg = new Pager();
objPg.PagerCollections().ForEach( delegate(Pager p)
{
TableRow tbr = new TableRow();
tbr.ID = " tr " + p.ID.ToString();
TableCell tc0 = new TableCell();
tc0.Text = p.ID.ToString();
tc0.BorderWidth = Unit.Pixel( 1);
tbr.Cells.Add(tc0);
TableCell tc1 = new TableCell();
tc1.Text = p.Size;
tc1.BorderWidth = Unit.Pixel( 1);
tbr.Cells.Add(tc1);
oTable.Rows.Add(tbr);
});
}
如果想在顯示於aspx頁面中,需要在aspx放一個asp:PlaceHolder容器,第12行。

1
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Default.aspx.cs
"
Inherits
=
"
_Default
"
%>
2
3 <! DOCTYPE html >
4
5 < html xmlns ="http://www.w3.org/1999/xhtml" >
6 < head runat ="server" >
7 < title ></ title >
8 </ head >
9 < body >
10 < form id ="form1" runat ="server" >
11 < div >
12 < asp:PlaceHolder ID ="PlaceHolder1" runat ="server" ></ asp:PlaceHolder >
13 </ div >
14 </ form >
15 </ body >
16 </ html >
2
3 <! DOCTYPE html >
4
5 < html xmlns ="http://www.w3.org/1999/xhtml" >
6 < head runat ="server" >
7 < title ></ title >
8 </ head >
9 < body >
10 < form id ="form1" runat ="server" >
11 < div >
12 < asp:PlaceHolder ID ="PlaceHolder1" runat ="server" ></ asp:PlaceHolder >
13 </ div >
14 </ form >
15 </ body >
16 </ html >
在.aspx.cs代碼頁的Page_Load事件寫,第11至14行代碼。

1
using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using Insus.NET;
8
9 public partial class _Default : System.Web.UI.Page
10 {
11 protected void Page_Load( object sender, EventArgs e)
12 {
13 DynamicCreateTable();
14 }
15 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using Insus.NET;
8
9 public partial class _Default : System.Web.UI.Page
10 {
11 protected void Page_Load( object sender, EventArgs e)
12 {
13 DynamicCreateTable();
14 }
15 }