动态创建DataTable和Table
2009-01-12 16:25 Virus-BeautyCode 阅读(4362) 评论(0) 编辑 收藏 举报
DataTable是在System.Data命名空间下面的类,用来以表格的形式存放数据,下面是动态创建并赋值的代码
Table是一种用来布局的控件,分为html和server两种,html静态的就是写html代码,很好办,server的要动态的用c#代码来创建和赋值,下面是一段示例代码
Code
DataTable dt = new DataTable();
//添加列
DataColumn Title = new DataColumn("Title", typeof(string));
DataColumn TitleLink = new DataColumn("TitleLink", typeof(string));
dt.Columns.Add(Title);
dt.Columns.Add(TitleLink);
//添加行数据
foreach (SPListItem item in items)
{
DataRow row = dt.NewRow();
row[Title] = item.GetFormattedValue("Title");
row[TitleLink] = SiteName + "/" + WebName + "/" + web.Lists[ListName].RootFolder +
"/" + "DispForm.aspx?ID=" +
item.GetFormattedValue("ID") +
"&Source=" + SiteName + "/" + WebName + "/"; ;
dt.Rows.Add(row);
}
DataTable dt = new DataTable();
//添加列
DataColumn Title = new DataColumn("Title", typeof(string));
DataColumn TitleLink = new DataColumn("TitleLink", typeof(string));
dt.Columns.Add(Title);
dt.Columns.Add(TitleLink);
//添加行数据
foreach (SPListItem item in items)
{
DataRow row = dt.NewRow();
row[Title] = item.GetFormattedValue("Title");
row[TitleLink] = SiteName + "/" + WebName + "/" + web.Lists[ListName].RootFolder +
"/" + "DispForm.aspx?ID=" +
item.GetFormattedValue("ID") +
"&Source=" + SiteName + "/" + WebName + "/"; ;
dt.Rows.Add(row);
}
Table是一种用来布局的控件,分为html和server两种,html静态的就是写html代码,很好办,server的要动态的用c#代码来创建和赋值,下面是一段示例代码
Code
private Table createTable()
{
Table table = new Table();
TableRow tr1 = new TableRow();
TableCell cell11 = new TableCell();
cell11.Text = "公告标题";
tr1.Cells.Add(cell11);
TableCell cell12 = new TableCell();
TextBox bulletinTitle = new TextBox();
bulletinTitle.ID = "bulletinTitle";
bulletinTitle.Text = "";
cell12.Controls.Add(bulletinTitle);
tr1.Cells.Add(cell12);
TableCell cell13 = new TableCell();
cell13.Text = "公告发布时间";
tr1.Cells.Add(cell13);
TableCell cell14 = new TableCell();
DateTimeControl bulletinReleaseDTBegin = new DateTimeControl();
bulletinReleaseDTBegin.ID = "bulletinReleaseDT";
bulletinReleaseDTBegin.DateOnly = true;
cell14.Controls.Add(bulletinReleaseDTBegin);
tr1.Cells.Add(cell14);
TableCell cell15 = new TableCell();
cell15.Text = "公告过期时间";
tr1.Cells.Add(cell15);
TableCell cell16 = new TableCell();
DateTimeControl bulletinExpireDTBegin = new DateTimeControl();
bulletinExpireDTBegin.DateOnly = true;
bulletinExpireDTBegin.ID = "bulletinExpireDTBegin";
cell16.Controls.Add(bulletinExpireDTBegin);
tr1.Cells.Add(cell16);
TableCell cell17 = new TableCell();
Button btnQuery = new Button();
btnQuery.ID = "btnQuery";
btnQuery.Text = "查询";
cell17.Controls.Add(btnQuery);
tr1.Cells.Add(cell17);
table.Rows.Add(tr1);
TableRow tr2 = new TableRow();
TableCell cell21 = new TableCell();
cell21.Text = "公告内容";
tr2.Cells.Add(cell21);
TableCell cell22 = new TableCell();
TextBox bulletinContent = new TextBox();
bulletinContent.ID = "bulletinContent";
bulletinContent.Text = "";
tr2.Cells.Add(cell22);
TableCell cell23 = new TableCell();
cell23.Text = "到";
tr2.Cells.Add(cell23);
TableCell cell24 = new TableCell();
DateTimeControl bulletinReleaseDTEnd = new DateTimeControl();
bulletinReleaseDTEnd.ID = "bulletinReleaseDTEnd";
bulletinReleaseDTEnd.DateOnly = true;
cell24.Controls.Add(bulletinReleaseDTEnd);
tr1.Cells.Add(cell24);
TableCell cell25 = new TableCell();
cell25.Text = "到";
tr2.Cells.Add(cell25);
TableCell cell26 = new TableCell();
DateTimeControl bulletinExpireDTEnd = new DateTimeControl();
bulletinExpireDTEnd.DateOnly = true;
bulletinExpireDTEnd.ID = "bulletinExpireDTEnd";
cell26.Controls.Add(bulletinExpireDTEnd);
tr1.Cells.Add(cell26);
TableCell cell27 = new TableCell();
cell27.Text = "";
tr1.Cells.Add(cell27);
table.Rows.Add(tr2);
return table;
}
private Table createTable()
{
Table table = new Table();
TableRow tr1 = new TableRow();
TableCell cell11 = new TableCell();
cell11.Text = "公告标题";
tr1.Cells.Add(cell11);
TableCell cell12 = new TableCell();
TextBox bulletinTitle = new TextBox();
bulletinTitle.ID = "bulletinTitle";
bulletinTitle.Text = "";
cell12.Controls.Add(bulletinTitle);
tr1.Cells.Add(cell12);
TableCell cell13 = new TableCell();
cell13.Text = "公告发布时间";
tr1.Cells.Add(cell13);
TableCell cell14 = new TableCell();
DateTimeControl bulletinReleaseDTBegin = new DateTimeControl();
bulletinReleaseDTBegin.ID = "bulletinReleaseDT";
bulletinReleaseDTBegin.DateOnly = true;
cell14.Controls.Add(bulletinReleaseDTBegin);
tr1.Cells.Add(cell14);
TableCell cell15 = new TableCell();
cell15.Text = "公告过期时间";
tr1.Cells.Add(cell15);
TableCell cell16 = new TableCell();
DateTimeControl bulletinExpireDTBegin = new DateTimeControl();
bulletinExpireDTBegin.DateOnly = true;
bulletinExpireDTBegin.ID = "bulletinExpireDTBegin";
cell16.Controls.Add(bulletinExpireDTBegin);
tr1.Cells.Add(cell16);
TableCell cell17 = new TableCell();
Button btnQuery = new Button();
btnQuery.ID = "btnQuery";
btnQuery.Text = "查询";
cell17.Controls.Add(btnQuery);
tr1.Cells.Add(cell17);
table.Rows.Add(tr1);
TableRow tr2 = new TableRow();
TableCell cell21 = new TableCell();
cell21.Text = "公告内容";
tr2.Cells.Add(cell21);
TableCell cell22 = new TableCell();
TextBox bulletinContent = new TextBox();
bulletinContent.ID = "bulletinContent";
bulletinContent.Text = "";
tr2.Cells.Add(cell22);
TableCell cell23 = new TableCell();
cell23.Text = "到";
tr2.Cells.Add(cell23);
TableCell cell24 = new TableCell();
DateTimeControl bulletinReleaseDTEnd = new DateTimeControl();
bulletinReleaseDTEnd.ID = "bulletinReleaseDTEnd";
bulletinReleaseDTEnd.DateOnly = true;
cell24.Controls.Add(bulletinReleaseDTEnd);
tr1.Cells.Add(cell24);
TableCell cell25 = new TableCell();
cell25.Text = "到";
tr2.Cells.Add(cell25);
TableCell cell26 = new TableCell();
DateTimeControl bulletinExpireDTEnd = new DateTimeControl();
bulletinExpireDTEnd.DateOnly = true;
bulletinExpireDTEnd.ID = "bulletinExpireDTEnd";
cell26.Controls.Add(bulletinExpireDTEnd);
tr1.Cells.Add(cell26);
TableCell cell27 = new TableCell();
cell27.Text = "";
tr1.Cells.Add(cell27);
table.Rows.Add(tr2);
return table;
}
Code
protected void Page_Load(object sender, EventArgs e)
{
createTable();
this.form1.Controls.Add(table);
}
protected void Page_Load(object sender, EventArgs e)
{
createTable();
this.form1.Controls.Add(table);
}