绑定List<T>到asp:Table控件
动态产生asp:Table控件,并加入PlaceHolder容器中,然后把List<T>数据绑定至asp:Table控件中显示。首先创建一个对象,这是个纸张对象。有ID和Size两个attribute。 在对象中,还产生一个Collection集合,存储纸张类型。
Pager
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了,内有注释。
DynamicCreateTable()
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行。
View Code
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行代码。
View Code
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 }