Hello,

I've created a table web server control that does what I want (imagine that!!). But my problem now is I would like to send this to a file anyone know how I can do that?

KG

Dani AI

Generated

As described, the right approach is to render the server-side Table into HTML and then write that HTML to disk. StreamWriter cannot directly serialize a Control object, but you can capture the control's rendered HTML with StringWriter + HtmlTextWriter and then save the resulting string. was correct to ask for details — here are concrete examples and common gotchas.

Example: render an existing control and save it

using System.IO;
using System.Web.UI;

// capture rendered HTML
var sw = new StringWriter();
var hw = new HtmlTextWriter(sw);
myTable.RenderControl(hw);
string html = sw.ToString();

// write to file (use Server.MapPath or HttpContext.Current.Server.MapPath)
File.WriteAllText(Server.MapPath("~/App_Data/myTable.html"), html, System.Text.Encoding.UTF8);

If the control was created purely in code and not in a page control tree, add it to a temporary Page before rendering (some controls expect a page/form context):

var page = new Page();
page.Controls.Add(myTable);
using (var sw = new StringWriter())
using (var hw = new HtmlTextWriter(sw))
{
    page.RenderControl(hw);
    File.WriteAllText(Server.MapPath("~/App_Data/myTable.html"), sw.ToString(), System.Text.Encoding.UTF8);
}

Notes and troubleshooting

  • If you get "must be placed inside a form tag with runat=server" when rendering GridView-like controls, add an empty override in the page: public override void VerifyRenderingInServerForm(Control control) { }.
  • Call DataBind() first for data-bound controls so their rows are generated before RenderControl.
  • Ensure the target folder (App_Data is a safe choice) is writable by the ASP.NET process (check the AppPool identity permissions).
  • Inspect sw.ToString() in debug to confirm HTML is present before writing.
  • For exports (CSV, Excel) it is often better to generate plain text/CSV from the data rather than HTML.

This method is stable for ASP.NET WebForms and will let you save the exact HTML output your page would send to the browser.

It's really not clear what you are trying to do...Could you give some more details?

-sypher

Obviously I'm using ASP.NET to create my pages. Instead of using a data grid I put a table on my web form and in my code I created a table through a loop that looks the way I want. I need to have my code written to a file. I'm able to use streamwriter for my other parts, but am not able to get streamwriter to write the table to a file. I was wondering if anyone knew if there was anyway to have the table written to a file. Is this a little more clear?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.