I am dynami...">

ASP.NET Get html table

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

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

I am dynamically adding content to the table and it works great. However, I want to get HTML tables after adding dynamic content, i.e. Something like this (formatting is not important, I just added)

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

I need the result as a string. Obviously, I could make a loop and build my own data table, but I would prefer not to, if at all possible.

+3
source share
6 answers

InnerHtml InnerText, HtmlTable.

, , Render? - ( )?

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

, , , ..

+6

. javascript - ( ) , .

<div id="tablediv">
<table>...</table>
</div>

JavaScript:

var html = document.getElementById('tablediv').innerHTML;
document.getElementById('hfTableHtml').value = html;

EDIT: , , ! w/something else,

0

, RenderControl :

    public static string GetRenderResult(Control control) {
        using(StringWriter sw = new StringWriter(CultureInfo.InvariantCulture)) {
            using(HtmlTextWriter writer = new HtmlTextWriter(sw))
                control.RenderControl(writer);
            sw.WriteLine();
            return sw.ToString();
        }
    }
0

There are several ways I can think of how to do this. It would be easy to surround the table in a. Then on the vb / C # side, just call hold.innerhtml and it will return a string.

0
source

I used the OnLoadfollowing table to HTML string conversion in my page method :

    string html;
    using (var writer = new StringWriter())
    using (var xmlwriter = new HtmlTextWriter(writer))
    {
        this.resultsTable.RenderControl(xmlwriter);
        html = writer.ToString();
    }
0
source

Take a look at HTMLAgilityPack (several SO posts link to it.)

0
source

Source: https://habr.com/ru/post/1758969/


All Articles