Table Web 服务器控件

   1.Table、TableRow 和 TableCell:

Table 控件可按编程方式生成表格,方法是将 TableRows 添加到表的 Rows 集合,并将 TableCells 添加到行的 Cells 集合。通过将控件添加到单元格的 Controls 集合,可按编程方式将内容添加到表单元格中。 

             动态添加行和单元格


TableRow表示 Table 控件中的行。

TableCell表示 Table 控件中的单元格

TableHeaderCell表示 Table 控件中的标题单元格

TableItemStyle 表示呈现为 TableRow 或 TableCell 的控件元素的样式属性


   int numrows = int.Parse(DropDown1.SelectedItem.Value);
            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()));
                    r.Cells.Add(c);
                }
               
                Table1.Rows.Add(r);
            }



动态添加文本和控件

// Total number of rows.
 int rowCnt;
 // Current row count.
 int rowCtr;
 // Total number of cells per row (columns).
 int cellCtr;
 // Current cell counter.
 int cellCnt;

 rowCnt = int.Parse(TextBox1.Text);
 cellCnt = int.Parse(TextBox2.Text);

 for(rowCtr=1; rowCtr <= rowCnt; rowCtr++) {
 // Create a new row and add it to the table.
 TableRow tRow = new TableRow();
 Table1.Rows.Add(tRow);
 for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++) {
 // Create a new cell and add it to the row.
 TableCell tCell = new TableCell();
 tRow.Cells.Add(tCell);
 // Mock up a product ID.
 string prodID = rowCtr + "_" + cellCtr;

 // Add a literal text as control.
 tCell.Controls.Add(new LiteralControl("Buy: "));
 // Create a Hyperlink Web server control and add it to the cell.
 System.Web.UI.WebControls.HyperLink h = new HyperLink();
 h.Text = rowCtr + ":" + cellCtr;
 h.NavigateUrl = "http://www.microsoft.com/net";
 tCell.Controls.Add(h);
 }
 }





 

posted on 2006-06-13 03:41  彭灿明  阅读(470)  评论(0)    收藏  举报

导航