Saving Rendered ASP.NET controls to files
This is a pattern I use frequently -- for example, if I have rendered some RSS format search results to a page, I may want to save this generated HTML of the DataList (or a Gridview, or any other UI display control) to a file, which can easily be read back in later and assigned to an HtmlGeneric Control that is then attached to a placeholder on an ASPX page, kind of like a FileSystem Caching mechanism: this.DataList1.DataBind(); System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); DataList1.RenderControl(oHtmlTextWriter); StreamWriter sr = null; string fullFilePath = Server.MapPath("mySavedDataList1.htm"); try { sr = new StreamWriter(fullFilePath); string oStuff = oStringWriter.ToString(); sr.Write(oStuff) } catch (Exception ex) { // Exception Handler here finally { if (sr != null) { sr.Close(); sr.Dispose(); } } What this does is wrap...