ASP.NET获取HTML表 [英] ASP.NET Get table html

查看:114
本文介绍了ASP.NET获取HTML表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的ASP.net页是这样一个表格:

I have a table on my ASP.net page something like this:

<table runat="server" id="resultsTable"></table>

我动态添加内容到表,它工作得很好。但是,我想要得到的表的HTML一旦我已经添加了动态内容,即是这样的(格式并不重要,我只是添加)

I dynamically add content to the table, and it works just fine. However, I want to get the HTML of the table once I've added the dynamic content, i.e. something like this (formatting isn't important, I've just added it)

<table runat="server" id="resultsTable">
  <tr>
    <td>Hello!</td>
  </tr>
  <tr>
    <td>Goodbye!</td>
  </tr>
</table>

我需要的结果作为一个字符串。很显然,我可以做一些循环,并建立自己的表中的数据,但我preFER不这样做,如果在所有可能的。

I need the result as a string. Obviously I could do some looping and build my own table with the data, but I'd prefer to not do that if at all possible.

谢谢!

推荐答案

起初我虽然只使用的innerHTML 或<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlcontainercontrol.innertext%28v=VS.71%29.aspx\"相对=nofollow>的InnerText 的方法,但是这些都是<一href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmltable.innerhtml%28v=VS.71%29.aspx\"相对=nofollow>不支持HTMLTABLE类。

Initially I though to just use the InnerHtml or InnerText methods, but these are not supported on the HtmlTable class.

那么,如果我们使用Render方法?像这样的东西(从拿阿纳托利Lubarsky )?

So what if we use the Render method? Something like this (take from Anatoly Lubarsky)?

public string RenderControl(Control ctrl) 
{
    StringBuilder sb = new StringBuilder();
    StringWriter tw = new StringWriter(sb);
    HtmlTextWriter hw = new HtmlTextWriter(tw);

    ctrl.RenderControl(hw);
    return sb.ToString();
}

此方法可明显被清理处理收盘作家等。

This method could obviously be cleaned up to handle closing the writers, etc.

这篇关于ASP.NET获取HTML表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆