0

I’m currently working on an asp.net page using C#. This page contains a button and once you click you will get a small HTML table with a name of person, cell phone number and email address. What I want to do is in code behind capture this HTML table along with its data in memory stream or other type of streams in order to do some operations. Here's my code

<table id="tb" runat="server">
        <tr> 
            <td> Name </td>
            <td> <asp:Label ID="lblName" runat="server" ></asp:Label>  </td>
        </tr>
         <tr> 
            <td> Phone </td>
            <td> <asp:Label ID="lblPhone" runat="server" ></asp:Label>  </td>
        </tr>
         <tr> 
            <td> Email </td>
            <td> <asp:Label ID="lblEmail" runat="server" ></asp:Label>  </td>
        </tr>
     </table> 

So please, if anyone could help of how to accomplish this process and I will be so thankful

7
  • 1
    Sorry if I misunderstood your question but don't you have the table in your Page object already? Commented Apr 10, 2012 at 13:36
  • where this HTML table is coming from AND what have you tried? Commented Apr 10, 2012 at 13:36
  • I basically fetch some data out of database and bound them Commented Apr 10, 2012 at 13:43
  • Are you saying you want to capture the raw html in a stream? Commented Apr 10, 2012 at 13:45
  • I want to capture the rendered HTML table into a stream Commented Apr 10, 2012 at 13:46

1 Answer 1

1

Well although I don't really understand why do you want to do that, you can do this pretty simply, you have to put id and runat server tag at your table, and you already have that, and then render this control to string :

markup:

<form id="form1" runat="server">
  <table id="tb" runat="server">
    <tr> 
        <td> Name </td>
        <td> <asp:Label ID="lblName" runat="server" ></asp:Label>  </td>
    </tr>
     <tr> 
        <td> Phone </td>
        <td> <asp:Label ID="lblPhone" runat="server" ></asp:Label>  </td>
    </tr>
     <tr> 
        <td> Email </td>
        <td> <asp:Label ID="lblEmail" runat="server" ></asp:Label>  </td>
    </tr>
  </table> 

  <hr />
  Rendered table :
  <hr />
  <asp:Label ID="lblRenderedTable" runat="server"></asp:Label>
  <hr />

</form>

code behind:

protected void Page_Load(object sender, EventArgs e)
{

  lblName.Text = "User Name";
  lblEmail.Text = "[email protected]";
  lblPhone.Text = "555-4214";

  StringBuilder sb = new StringBuilder();
  StringWriter tw = new StringWriter(sb);
  HtmlTextWriter hw = new HtmlTextWriter(tw);
  tb.RenderControl(hw);
  string tableContents = sb.ToString();

  lblRenderedTable.Text = tableContents;
}
Sign up to request clarification or add additional context in comments.

3 Comments

it didn't work. the string variable shows me an empty string !!
Well maybe this happens because I extended the table to have some asp labels which prevent the complete rendering. When I tried it on a raw HTML table it worked fine. However, when I tried it on a table contains some asp labels it didn't work. So if you have an advice I will be so thanful
well, I don't know, for me it works with label also, maybe you didn't set label values before rendering table contents to string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.