Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I convert a dynamically created c# table to a html string?

Tags:

html

c#

Can I convert a dynamically created c# table to an html string ?

I mean like this;

Table t = new Table();
TableRow tr = new TableRow();
TableCell td = new TableCell();
td.Text = "Some text... Istanbul";
tr.Cells.Add(td);
t.Rows.Add(tr);
t.ToString();
Response.Write(t.ToString());

I wanna see in the page;

<table> <tr> <td> Some text...
 Istanbul </td> <tr> </table>
like image 336
Bilgin Kılıç Avatar asked Oct 06 '09 07:10

Bilgin Kılıç


1 Answers

using (StringWriter sw = new StringWriter())
{
  Table t = new Table();
  TableRow tr = new TableRow();
  TableCell td = new TableCell {Text = "Some text... Istanbul"};

  tr.Cells.Add(td);
  t.Rows.Add(tr);

  t.RenderControl(new HtmlTextWriter(sw));

  string html = sw.ToString();
}

result:

<table border="0"><tr><td>Some text... Istanbul</td></tr></table>

like image 162
CD.. Avatar answered Oct 08 '22 09:10

CD..