2011-12-02 71 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); 

其中四舍五入为你的餐桌。