2011-12-02 101 views
1

我有這樣的HTML代碼添加行HTML表

<table id="rounded" runat=server style="position:relative;left:-45px;" > 
    <thead> 
     <tr> 
      <th scope="col" class="rounded-company">status</th> 
     <th scope="col" class="rounded-q1">date</th> 

      <th scope="col" class="rounded-q1">price</th> 
      <th scope="col" class="rounded-q1">quantity</th> 
       <th scope="col" class="rounded-q1">option1</th> 
       <th scope="col" class="rounded-q1">option2</th> 
       <th scope="col" class="rounded-q1">paync</th> 
       <th scope="col" class="rounded-q1">product_id</th> 

        <th scope="col" class="rounded-q1">sell number</th> 
        <th scope="col" class="rounded-q4"> name</th> 


     </tr> 
    </thead> 
     <tfoot> 
     <tr> 
      <td colspan="9" class="rounded-foot-left" dir=rtl ><em></em></td> 
      <td class="rounded-foot-right">&nbsp;</td> 
     </tr> 
    </tfoot> 

</table> 

在塞雷爾語身邊,我想這個字符串添加到錶行

rows="<tr><td>" & reader4.GetValue(9) & "</td>" & "<td>" & reader4.GetValue(8) & "</td>" & "<td>" & reader4.GetValue(7) & "</td>" & _ 
         "<td>" & reader4.GetValue(3) & "</td>" & "<td>" & tempoption & "</td>" & "<td>" & tempoption2 & "</td>" & _ 
          "<td>" & reader4.GetValue(6) & "</td>" & "<td>" & reader4.GetValue(2) & "</td>" & "<td>" & reader4.GetValue(1) & "</td>" & "<td>" & reader4.GetValue(0) & "</td>" & "</tr>" 

在javascript中很sipmle

$("#rounded").html($("#rounded").html()+str); 

但asp.net不支持InnerHTML表格

這項任務的正確方法是什麼?

回答

2

不要將標記添加到服務器端表中 - 在後面的代碼中添加HtmlTableRow

C#:

var tr = new HtmlTableRow(); 
tr.Cells.Add(new HtmlTableCell(reader4.GetValue(9)); 
tr.Cells.Add(new HtmlTableCell(reader4.GetValue(8)); 
... 

rounded.Rows.Add(tr); 

VB.NET:

Dim tr as HtmlTableRow = New HtmlTableRow() 
tr.Cells.Add(New HtmlTableCell(reader4.GetValue(9)) 
tr.Cells.Add(New HtmlTableCell(reader4.GetValue(8)) 
... 

rounded.Rows.Add(tr) 
+0

嗨Oded,感謝重播 - 我需要代碼在服務器端(VB)和您的代碼是javascript – baaroz

+0

@baaroz - 不,我的代碼是C#。而且您從未將VB.NET指定爲語言 - 下次請正確標記。用VB.NET例子回答更新。 – Oded

0

你可以有一個表作爲服務器對象

  1. //生成行和單元格。

    int numrows = 3; 
        int numcells = 2; 
        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("row " 
           + j.ToString() + ", cell " + i.ToString())); 
          r.Cells.Add(c); 
         } 
         Table1.Rows.Add(r); 
        } 
    
0

你是對的,我檢查了它。 但是你仍然可以使用Rows.Add方法爲你的表。 我試着和它的作品:

HtmlTableRow a = new HtmlTableRow();    

     HtmlTableCell cell = new HtmlTableCell(); 
     cell.InnerHtml = "test"; 
     a.Cells.Add(cell); 

     HtmlTableCell cell2 = new HtmlTableCell(); 
     cell2.InnerHtml = "test"; 
     a.Cells.Add(cell2); 
     HtmlTableCell cell3 = new HtmlTableCell(); 
     cell3.InnerHtml = "test"; 
     a.Cells.Add(cell3); 
     HtmlTableCell cell4 = new HtmlTableCell(); 
     cell4.InnerHtml = "test"; 
     a.Cells.Add(cell4); 
     HtmlTableCell cell5 = new HtmlTableCell(); 
     cell5.InnerHtml = "test"; 
     a.Cells.Add(cell5); 
     HtmlTableCell cell6 = new HtmlTableCell(); 
     cell6.InnerHtml = "test"; 
     a.Cells.Add(cell6); 
     HtmlTableCell cell7 = new HtmlTableCell(); 
     cell7.InnerHtml = "test"; 
     a.Cells.Add(cell7); 
     HtmlTableCell cell8 = new HtmlTableCell(); 
     cell8.InnerHtml = "test"; 
     a.Cells.Add(cell8); 
     HtmlTableCell cell9 = new HtmlTableCell(); 
     cell9.InnerHtml = "test"; 
     a.Cells.Add(cell9); 
     HtmlTableCell cell10 = new HtmlTableCell(); 
     cell10.InnerHtml = "test"; 
     a.Cells.Add(cell10); 


     ((HtmlTable)rounded).Rows.Add(a); 

其中四捨五入爲你的餐桌。