2013-01-05 39 views
1

更多的新手:)從代碼中添加行現有的HTML桌子後面

我用下面的HTML代碼創建的HTML表格的

<table runat="server" id="Table1" border="1" class="style1"> 
    <tr> 
     <td class="style2" colspan="3"> 
      Info.</td> 
     <td class="style2" colspan="2"> 
      TypeA</td> 
     <td class="style2"> 
      TypeB</td> 
     <td class="style2" rowspan="2"> 
      TypeC</td> 
    </tr> 
    <tr> 
     <td class="style3"> 
      Dept.</td> 
     <td class="style3"> 
      Div.</td> 
     <td class="style3"> 
      Unit</td> 
     <td class="style3"> 
      TypeA.1</td> 
     <td class="style3"> 
      TypeA.1</td> 
     <td style="text-align: center"> 
      TypeB.1</td> 
    </tr> 
</table> 

我試圖添加一行它使用

protected void Button1_Click(object sender, EventArgs e) 
{ 
    TableRow tRow = new TableRow(); 
    for (int i = 1; i < 8; i++) 
    { 
     TableCell tb = new TableCell(); 
     tb.Text = "text"; 
     tRow.Controls.Add(tb); 
    } 
    Table1.Rows.Add(tRow); 
} 

但我得到:

最好的超載對於「System.Web.UI.HtmlControls.HtmlTableRowCollection.Add(System.Web.UI.HtmlControls.HtmlTableRow)」 ED方法匹配具有一些無效參數

我用Google搜索周圍,但沒有運氣。

我這樣做是爲了避免列標題合併行爲在代碼後面,而我仍然需要尋找如何合併行標題。這是完成https://stackoverflow.com/questions/13670384/asp-net-dynamic-input-display-table-by-section-with-multi-columns-rows-headers

,..接近標題視圖中的最後一個步驟是:創建將有應合併是否與上述行相同的轉發標題細胞

enter image description here

行。 Appreaciate從StackOverflow成員偉大的助理。

+0

不確定,但可能是現有表格行的行類型。或排應該是三列 –

回答

4

錯誤在例外情況下很明顯。您正在使用TableRow而不是HtmlTableRow
將鼠標懸停在TableRow tRow = new TableRow();上查看哪個命名空間是TableRow類。

細胞的類似變化是必需的。即使用HtmlTableCell而不是TableCell

編輯:Table,TableRow是來自System.Web.UI.WebControls名稱空間的類。
HtmlTable,HtmlTableRow是來自System.Web.UI.HtmlControls命名空間的類。

+0

這是偉大的兄弟Shahkalpesh ......多麼優秀的知識。一個小問題,爲什麼如果我再次單擊按鈕,它不會產生另一行?我還會利用問你是否知道如何合併行,如果它與同一列中的前一行相同......你真的迷戀傢伙。 –

0

你只需要更多的關注。你可以試試這個:

第1步:

<table runat="server" id="Table1" border="1" class="style1"> 
    <tr> 
     <td class="style2" colspan="3"> 
      Info.</td> 
     <td class="style2" colspan="2"> 
      TypeA</td> 
     <td class="style2"> 
      TypeB</td> 
     <td class="style2" rowspan="2"> 
      TypeC</td> 
    </tr> 
    <tr> 
     <td class="style3"> 
      Dept.</td> 
     <td class="style3"> 
      Div.</td> 
     <td class="style3"> 
      Unit</td> 
     <td class="style3"> 
      TypeA.1</td> 
     <td class="style3"> 
      TypeA.1</td> 
     <td style="text-align: center"> 
      TypeB.1</td> 
    </tr> 
</table> 

第2步:使用後面的代碼驗證碼:

HtmlTableRow tRow = new HtmlTableRow(); 
for (int i = 1; i < 3; i++) 
{ 
    HtmlTableCell tb = new HtmlTableCell();    
    tb.InnerText = "text"; 
    tRow.Controls.Add(tb); 
} 
Table1.Rows.Add(tRow); 

注意:

using System.Web.UI.HtmlControls; 
:在後面的代碼使用此 using
相關問題