博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

动态改变表格的行数列数(添加表格)

Posted on 2006-10-21 16:40  孤峰皓月  阅读(1558)  评论(0编辑  收藏  举报
 1<%@ Page Language="C#" %>
 2
 3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4
 5<script runat="server">
 6
 7    protected void Page_Load(object sender, EventArgs e)
 8    {
 9        
10    }

11
12    protected void Button1_Click(object sender, EventArgs e)
13    {
14        int rn = int.Parse(rows.SelectedValue);//得到需要增加的行数
15        int cn = int.Parse(cell.SelectedValue);//得到需要增加的列数
16
17        for (int i = 1; i <= rn; i++)//
18        {//进入外部循环,也就是循环增加表行
19            TableRow tr = new TableRow();//申明一个表格行
20            for (int j = 1; j <= cn; j++)
21            {//进入内部循环,以增加表格列
22                TableCell tc = new TableCell();//申明一个表格列
23                if (i == 1)
24                {//如果是第一行,就增加下面的字
25                    tc.Controls.Add(new LiteralControl("洪川"));
26                }

27                else
28                {//否则
29                    tc.Controls.Add(new LiteralControl(i.ToString() + "," + j.ToString()));//在表格里增加当前是行,列坐标
30                }

31                tr.Cells.Add(tc);//把列增加到行里面去
32            }

33            Table1.Rows.Add(tr);//把行增加到表里去
34        }

35    }

36</script>
37
38<html xmlns="http://www.w3.org/1999/xhtml" >
39<head runat="server">
40    <title>无标题页</title>
41</head>
42<body>
43    <form id="form1" runat="server">
44    <div>
45        Table ,这个有点HTML基础的人都知道了,是表格的意思啦,也是布局的一个重要方法,如果是用DW的话,你就会知道他有多重要了!<br />
46        而VS推出的Table服务器控件最大的特色当推他可以动态是控制表格的行列数,下面还是做个演示:<br />
47        <br />
48        <strong>
49        演示一: 动态添加表格行和列,在特定的格里写特定的字,并在当前单元格里面把坐标写出来</strong><br />
50        <asp:Table ID="Table1" runat="server" BorderColor="Black" BorderStyle="Dashed" BorderWidth="1px" GridLines="Both">
51        </asp:Table>
52        <asp:DropDownList ID="rows" runat="server">
53            <asp:ListItem>1</asp:ListItem>
54            <asp:ListItem>2</asp:ListItem>
55            <asp:ListItem>3</asp:ListItem>
56            <asp:ListItem>4</asp:ListItem>
57        </asp:DropDownList>
58        <asp:DropDownList ID="cell" runat="server">
59            <asp:ListItem>1</asp:ListItem>
60            <asp:ListItem>2</asp:ListItem>
61            <asp:ListItem>3</asp:ListItem>
62            <asp:ListItem>4</asp:ListItem>
63            <asp:ListItem>5</asp:ListItem>
64        </asp:DropDownList>
65        <asp:Button ID="Button1" runat="server" Text="给我建个表格来" OnClick="Button1_Click" /><br />
66        <br />
67    </div>
68    </form>
69</body>
70</html>
71

     int rn = int.Parse(rows.SelectedValue);//得到需要增加的行数
      int cn = int.Parse(cell.SelectedValue);//得到需要增加的列数

 先获取要创建表格的行数列数:
 int rn = int.Parse(rows.SelectedValue);
 int cn = int.Parse(cell.SelectedValue);

然后申明表格行和表格列:
TableRow tr = new TableRow();
TableCell tc = new TableCell();

给表格内容赋值:
tc.Controls.Add(new LiteRalControl("表格行内容"));

 tr.Cells.Add(tc);//把列增加到行里面去

添加列到行中去:
tr.Cells.Add(tc);
添加行到表格中:
Table1.Rows.Add(tr);