a.前台:定义数据绑定 <ASP:Label ID="labTable" Text="<%# SetTable() %>" runat="server" />
后台:拼接数据 Page_Load 中调用: labTable.DataBind();
//设置动态table,可变的字段添加数据
b.利用控件 <asp:PlaceHolder id="Place" runat="server"/> 再创建HtmlTable table = new HtmlTable(); Place.Controls.Add(table); //添加到控件
后台:拼接数据 Page_Load 中调用: labTable.DataBind();
//设置动态table,可变的字段添加数据
public
string
SetTable()
... {
string strTbHead = "<table border='0' cellpadding='0' cellspacing='0' width='100%' height='100%'>";
string strTbTail = "</table>";
string strTbBody = "";
string[,] aryTbValue = new string[20, 3]; //aryTbValue[0,0]:名字,aryTbValue[0,1]控件名,aryTbValue[0,2]类型
for(int i = 0; i < aryTbValue.GetLength(0); i++) ...{
aryTbValue[i, 0] = "名字"+i;
aryTbValue[i, 1] = "name"+i;
aryTbValue[i, 2] = "nvarchar";
}
//列数和行数
int columnCount = 2;
int rowCount = ((aryTbValue.GetLength(0) % columnCount) == 0) ? (aryTbValue.GetLength(0) / columnCount) : (aryTbValue.GetLength(0) / columnCount) + 1;
int index = 0;
for(int i=0; i<rowCount; i++)...{
strTbBody += "<tr height='20'>";
for(int j = 0; j < columnCount && index < aryTbValue.GetLength(0); j++) ...{
switch(aryTbValue[index, 2]) ...{
case "nvarchar":
strTbBody += "<td align='right' width='20%'>" + aryTbValue[index, 0] + "</td>";
strTbBody += "<td align='right' width='20%'><input type='text' name='" + aryTbValue[index, 1] + "'/></td>";
break;
}
index++;
}
strTbBody += "</tr>";
}
return strTbHead + strTbBody + strTbTail;
}
... {
string strTbHead = "<table border='0' cellpadding='0' cellspacing='0' width='100%' height='100%'>";
string strTbTail = "</table>";
string strTbBody = "";
string[,] aryTbValue = new string[20, 3]; //aryTbValue[0,0]:名字,aryTbValue[0,1]控件名,aryTbValue[0,2]类型
for(int i = 0; i < aryTbValue.GetLength(0); i++) ...{
aryTbValue[i, 0] = "名字"+i;
aryTbValue[i, 1] = "name"+i;
aryTbValue[i, 2] = "nvarchar";
}
//列数和行数
int columnCount = 2;
int rowCount = ((aryTbValue.GetLength(0) % columnCount) == 0) ? (aryTbValue.GetLength(0) / columnCount) : (aryTbValue.GetLength(0) / columnCount) + 1;
int index = 0;
for(int i=0; i<rowCount; i++)...{
strTbBody += "<tr height='20'>";
for(int j = 0; j < columnCount && index < aryTbValue.GetLength(0); j++) ...{
switch(aryTbValue[index, 2]) ...{
case "nvarchar":
strTbBody += "<td align='right' width='20%'>" + aryTbValue[index, 0] + "</td>";
strTbBody += "<td align='right' width='20%'><input type='text' name='" + aryTbValue[index, 1] + "'/></td>";
break;
}
index++;
}
strTbBody += "</tr>";
}
return strTbHead + strTbBody + strTbTail;
}
b.利用控件 <asp:PlaceHolder id="Place" runat="server"/> 再创建HtmlTable table = new HtmlTable(); Place.Controls.Add(table); //添加到控件