yueyue, I have fallen in love with you for

2011-02-09, 01:18:25 PM

asp.net中用代码动态创建表格的完整代码

Posted on 2011-09-02 17:16  张超的博客  阅读(638)  评论(0)    收藏  举报

表头设计代码:

View Code
  private void TableInit()
{
string[] TableHeader = { "角色编号", "角色名称", "角色说明", "删除", "修改" };
TableRow tr
= new TableRow();
tr.HorizontalAlign
= HorizontalAlign.Center;

for (int i = 0; i < 5; i++)
{
TableCell tc
= new TableCell();
tc.Height
= 10;
tc.BorderWidth
= 0;

tc.Style[
"background-color"] = "PowderBlue";
tc.Style[
"font-size"] = "12px";
tc.Style[
"font-weight"] = "bold";
tc.Style[
"color"] = "black";
tc.Text
= TableHeader[i];

tr.Cells.Add(tc);

tc.Width
= Unit.Parse("20%");
}

Table1.Rows.Add(tr);
}

 

表尾代码如下:

View Code
 private void TableFinish()
{
TableRow tr
= new TableRow();
TableCell tc
= new TableCell();
tc.Height
= 10;
tc.ColumnSpan
= 15;
tc.Style[
"background-color"] = "PowderBlue";
tr.Cells.Add(tc);
Table1.Rows.Add(tr);
}

 

表格单元格代码如下:

 

View Code
 private TableCell NewTableCell()
{
TableCell tc
= new TableCell();
tc.Height
= 10;
tc.BorderWidth
= 0;
tc.Style[
"background-color"] = "#FFFFFF";
tc.Style[
"font-size"] = "12px";
tc.Style[
"font-weight"] = "bold";
tc.Style[
"color"] = "#000000";
return tc;
}

 

组合代码如下:

View Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CurrentUser user
= (CurrentUser)Session["user"];

if (null == user)
{
Response.Redirect(
"default.htm");
return;
}

TableInit();
SystemRole SysRole
= new SystemRole();
SysRole.ConnectionString
= user.ConnectString;
//SysRole.RoleFunction.ConnectionString = user.ConnectString;

if (SysRole.QueryRole())
{
while (SysRole.GetNextRole())
{
TableRow tr
= new TableRow();
tr.HorizontalAlign
= HorizontalAlign.Center;

string TableId = "t" + SysRole.RoleItem.AUTO_ID.ToString();

TableCell tc1
= NewTableCell();
tc1.Text
= SysRole.RoleItem.ROLE_ID;
tc1.Attributes.Add(
"OnClick", "JavaScript:Show('" + TableId + "');");
tr.Cells.Add(tc1);

TableCell tc2
= NewTableCell();
tc2.Text
= SysRole.RoleItem.ROLE_NAME;
tr.Cells.Add(tc2);

TableCell tc3
= NewTableCell();
tc3.Text
= SysRole.RoleItem.REMARK;
tr.Cells.Add(tc3);

TableCell tc4
= NewTableCell();
tc4.Text
= "<input type='CheckBox' name='id' value='" + SysRole.RoleItem.AUTO_ID.ToString() + "' />删除";
tr.Cells.Add(tc4);

TableCell tc5
= NewTableCell();
tc5.Text
= "<input type='button' value='修改' onclick='JavaScript:ModifySysRole(" + SysRole.RoleItem.AUTO_ID.ToString() + ");' />";
tr.Cells.Add(tc5);

Table1.Rows.Add(tr);

TableRow tr1
= new TableRow();
TableCell tc6
= new TableCell();
tc6.ColumnSpan
= 5;
tc6.Width
= Unit.Parse("100%");
tc6.Style[
"background-color"] = "PowderBlue";
tc6.Style[
"font-size"] = "12px";
tc6.Style[
"font-weight"] = "bold";
tc6.Style[
"color"] = "#000000";

string Html = "<table id='" + TableId + "' width='100%' border='1' style='display:none'>";
Html
+= "<tr><td>功能编码</td><td>功能名称</td><td>功能编码</td><td>功能名称</td><td>功能编码</td><td>功能名称</td></tr><tr>";
int count = 1;
while (SysRole.RoleFunction.GetNextFunction())
{
Html
+= "<td>" + SysRole.RoleFunction.FuncItem.FUNC_ID + "</td>";
Html
+= "<td>" + SysRole.RoleFunction.FuncItem.FUNC_NAME + "</td>";
count
++;
if (count > 3)
{
Html
+= "</tr><tr>";
count
= 1;
}
}
Html
+= "</tr></table>";
tc6.Text
= Html;
tr1.Cells.Add(tc6);
Table1.Rows.Add(tr1);
}
TableFinish();
}
else
Response.Write(
"程序错误:" + SysRole.ReturnInfor);
}
}

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3