baaroz
baaroz

Reputation: 19587

adding rows to html table from server side

I have this html code

<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>

In serer side I want to add this string to the table rows

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>"

In javascript it very sipmle

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

but asp.net doesn't support InnerHTML for tables

What is the right approach for this task?

Upvotes: 1

Views: 10463

Answers (3)

Oded
Oded

Reputation: 498972

Don't add markup to a serverside table - add HtmlTableRows in the code behind.

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)

Upvotes: 2

ravenik
ravenik

Reputation: 852

You are right I checked it. But you still can use Rows.Add method for your table. I tried and it works:

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);  

where rounded is your table.

Upvotes: 0

Olev
Olev

Reputation: 67

You can have a table as a server object

  1. // Generate rows and cells.

        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);
        }
    

Upvotes: 0

Related Questions