asp.net动态表格生成(收藏)

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<mce:script runat="server"><!--
    void Page_Load(Object sender, EventArgs e) {

        int numrows = int.Parse(DropDown1.SelectedItem.Value);///为啥用int.parse?
        int numcells = int.Parse(DropDown2.SelectedItem.Value);
        for (int j = 0; j < numrows; j++) {
            TableRow r = new TableRow();
            for (int i = 0; i < numcells; i++)
            {
                TableCell c = new TableCell();
               
                c.Controls.Add(new LiteralControl("行" + j.ToString() + ", 列" + i.ToString()));///这句话啥意思?c.controls.add 和 new literalControl是啥意思?
                                                                                              ///
                r.Cells.Add(c);///这句话啥意思啊?
                   
            }
            Table1.Rows.Add(r);///这句话啥意思啊?
        }
    }
// --></mce:script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Table,TableRow,tablecell控件演示</title>
</head>
<body>
    <h3><font face="宋体">表的示例</font></h3>
    <form id="form1" runat="server">
    <div>
    <asp:Table ID="Table1" Font-Names="宋体" Font-Size="9pt" CellPadding="5" CellSpacing="0" BorderColor="black" BorderWidth="1" GridLines="Both" runat="server" />
    <p>
    表行:
    <asp:DropDownList ID="DropDown1" runat="server">
        <asp:ListItem Value="1">1</asp:ListItem>
        <asp:ListItem Value="2">2</asp:ListItem>
        <asp:ListItem Value="3">3</asp:ListItem>
        <asp:ListItem Value="4">4</asp:ListItem>
    </asp:DropDownList>
   
   
    表列
    <asp:DropDownList ID="DropDown2" runat="server">
        <asp:ListItem Value="1">1</asp:ListItem>
        <asp:ListItem Value="2">2</asp:ListItem>
        <asp:ListItem Value="3">3</asp:ListItem>
        <asp:ListItem Value="4">4</asp:ListItem>
    </asp:DropDownList>
   
    </p>
    <asp:Button Text="创建表" runat="server" ID="Button1" />
    </div>
    </form>
</body>
</html>

 

 

我的问题都在上面,大大们给的建议

  1. DropDown1.SelectedItem.Value为string型,要转化为int    
  2.   
  3. void Page_Load(Object sender, EventArgs e) {   
  4.   
  5.         int numrows = int.Parse(DropDown1.SelectedItem.Value);///将DropDown1值转换为int型   
  6.         int numcells = int.Parse(DropDown2.SelectedItem.Value);   
  7.         for (int j = 0; j < numrows; j++) {   
  8.             TableRow r = new TableRow();   
  9.             for (int i = 0; i < numcells; i++)   
  10.             {   
  11.                 TableCell c = new TableCell();   
  12.                 //TableCell 为单元格 c.Controls.Add 意思是在单元格中添加控件,new LiteralControl 创建了LiteralControl 控件   
  13.                   //显示第几行第几列   
  14.                 c.Controls.Add(new LiteralControl("行" + j.ToString() + ", 列" + i.ToString                                                                                          
  15.                 r.Cells.Add(c); //将Tablecell添加到table的row中                       
  16.             }   
  17.             Table1.Rows.Add(r);// 将行添加到table中   
  18.         }   
  19.     }   
  20.   
  21.   
  22. 就是动态来添加table的操作  
posted @ 2009-02-17 20:50  Devil_Zhang  阅读(600)  评论(0编辑  收藏  举报