HOW TO:將資料列和儲存格動態地加入至 Table Web 伺服器控制項

更新:2007 年 11 月

通常在執行階段時,會將資料列和儲存格加入至 Table Web 伺服器控制項。Table Web 伺服器控制項就是特別為這項工作所設計的。

注意事項:

若要在 [設計] 檢視中以視覺方式設計表格,請使用 HtmlTable 控制項。如果您還需要利用程式設計方式管理 HtmlTable 控制項的內容,請將其資料列和儲存格的 runat 屬性設定為 server,將它們轉換為 HtmlTableRowHtmlTableCell 控制項。如需詳細資訊,請參閱將 HTML 伺服器控制項轉換為 HTML 項目

Table Web 伺服器控制項中的資料列是 TableRow 型別的物件。Table 控制項的 Rows 屬性,支援 TableRow 物件集合。若要將資料列加入至表格,請將 TableRow 物件加入至此集合。

同樣地,TableRow 物件擁有支援 TableCell 型別物件集合的 Cells 屬性。您可以藉由處理此集合,將儲存格加入至列。

若要動態將資料列和儲存格加入至表格

  1. 若要加入資料列,請建立 TableRow 型別的新物件:

    Dim tRow As New TableRow()
    Table1.Rows.Add(tRow)
    
    TableRow tRow = new TableRow();
    Table1.Rows.Add(tRow);
    
  2. 若要將儲存格加入資料列,請建立 TableCell 型別的一個或多個物件:

    Dim tCell As New TableCell()
    tRow.Cells.Add(tCell)
    
    TableCell tCell = new TableCell();
    tRow.Cells.Add(tCell);
    
  3. 將內容加入至新的儲存格。您可以使用多種方法來執行此作業,如下表所示。

    若要加入

    請執行

    靜態文字

    設定儲存格的 Text 屬性。

    控制項

    宣告控制項的執行個體,並將它加入至儲存格的 Controls 集合。

    文字和控制項都位於相同的儲存格

    建立 Literal 類別的執行個體,以宣告文字。將它加入至儲存格的 Controls 集合,如同對其他控制項所做的一樣。

    注意事項:

    根據預設,動態加入至 Web Form 網頁的控制項,會加入至網頁的檢視狀態。如果您在每次的往返作業都重新建立控制項,這可能會在處理網頁時造成未預期的行為,因為檢視狀態會在重新建立控制項之前還原。您可以將容器控制項 (例如,Table 控制項) 的 EnableViewState 屬性設定為 false,避免發生問題。如需詳細資訊,請參閱以程式設計方式加入 ASP.NET 控制項

    下列程式碼範例會示範,如何將資料列和儲存格加入至 Table 控制項。列和欄的數量是由使用者輸入至兩個文字方塊的內容而定。每個儲存格將列和儲存格數量顯示為靜態文字。

    Protected Sub Button1_Click(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles Button1.Click
       ' Total number of rows.
       Dim rowCnt As Integer
       ' Current row count
       Dim rowCtr As Integer
       ' Total number of cells (columns).
       Dim cellCtr As Integer
       ' Current cell counter.
       Dim cellCnt As Integer
    
       rowCnt = CInt(Textbox1.Text)
       cellCnt = CInt(Textbox2.Text)
    
       For rowCtr = 1 To rowCnt
          Dim tRow As New TableRow()
          For cellCtr = 1 To cellCnt
             Dim tCell As New TableCell()
             tCell.Text = "Row " & rowCtr & ", Cell " & cellCtr
             ' Add new TableCell object to row.
             tRow.Cells.Add(tCell)
          Next
          ' Add new row to table.
          Table1.Rows.Add(tRow)
       Next
    
    End Sub
    
    protected void Button1_Click (object sender, System.EventArgs e)
    {
       // 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 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();
             tCell.Text = "Row " + rowCtr + ", Cell " + cellCtr;
             tRow.Cells.Add(tCell);
          }
       }
    }
    

    下列程式碼範例與前一個類似,但是會在每個儲存格中顯示靜態文字和 HyperLink 控制項。HyperLink 控制項會巡覽至模擬的 URL,傳遞模擬的產品 ID。因為範例混合了靜態文字和控制項,所以靜態文字實作為 Literal 物件,如同 HyperLink 控制項一樣地加入至儲存格的 Controls 集合。

    Protected Sub Button1_Click(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles Button1.Click
       ' Total number of rows.
       Dim rowCnt As Integer
       ' Current row count
       Dim rowCtr As Integer
       ' Total number of cells (columns).
       Dim cellCtr As Integer
       ' Current cell counter.
       Dim cellCnt As Integer
    
       rowCnt = CInt(TextBox1.Text)
       cellCnt = CInt(TextBox2.Text)
    
       For rowCtr = 1 To rowCnt
          Dim tRow As New TableRow()
          For cellCtr = 1 To cellCnt
             Dim tCell As New TableCell()
             ' Mock up a product ID
             Dim prodID As String
             prodID = rowCtr & "_" & cellCtr
    
             ' Create literal text as control.
             Dim s As New LiteralControl()
             s.Text = "Buy: "
             ' Add to cell.
             tCell.Controls.Add(s)
             ' Create Hyperlink Web Server control and add to cell.
             Dim h As New HyperLink()
             h.Text = rowCtr & ":" & cellCtr
             h.href = "https://www.microsoft.com/net"
             ' Add cell to row.
             tCell.Controls.Add(h)
             ' Add new TableCell object to row.
             tRow.Cells.Add(tCell) 
          Next cellCtr
          ' Add new row to table.
          Table1.Rows.Add(tRow) 
       Next rowCtr
    End Sub
    
    Protected void Button1_Click (object sender, System.EventArgs e)
    {
       // 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.href = "https://www.microsoft.com/net";
             tCell.Controls.Add(h);
          }
       }
    }
    

請參閱

參考

Table、TableRow 和 TableCell Web 伺服器控制項概觀