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
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
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
public override void VerifyRenderingInServerForm(Control control) { }. DataBind() first for data-bound controls so their rows are generated before RenderControl. sw.ToString() in debug to confirm HTML is present before writing. 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?
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.