c# - 我可以将动态创建的 c# 表转换为 html 字符串吗?

标签 c# html

我可以将动态创建的 c# 表转换为 html 字符串吗?

我的意思是这样;

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

我想在页面中看到;

<table> <tr> <td> Some text...
 Istanbul </td> <tr> </table>

最佳答案

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

结果:

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

关于c# - 我可以将动态创建的 c# 表转换为 html 字符串吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1524105/

相关文章:

html/css : how to create a hexagonal image-placeholder

c# - Squirrel.windows 在桌面上创建多个快捷方式

c# - 将 JObject 转换为 Dictionary<string, object>。可能吗?

html - 使用vba从网站抓取数据

html - 使用uikit的图像叠加悬停效果

javascript - 如何在渲染 PHP 对象时添加对 HTML 的 Javascript 引用?

c# - Continue as First Statement in Concise If Else

c# - Enumerator.MoveNext() 在第一次调用时抛出 'Collection was Modified'

c# - 新(修饰符)函数的实际用法?

JavaServlet : getting info from a DB and showing it on the screen