Take this scenario: On a website a visitor fills in a form after which the form is to be filed as a static html page You might have several reasons for wanting to do that, one of them are search engines Getting it done in asp.. .NET TOOK ME More Effort Thani Had Originally Expected. Let me Share What I have found to be working.
The easy way to catch the html rendered by asp.net is to save a page in the client browser. To catch it on the server you have to hook into the (html) rendering process. There is no Page.OnRender event but the Page class does have a Render method. The method has a protected visibility, you can not invoke it form your code. The Render method is invoked when asp.net renders the page to the output, to hook in you override the method. The Render method has a parameter of type htnltextwriter, The default is invoking base.render passing it the htmltextwriter object.
Protected Override Void Render (HTMLTextWriter Writer) {// default behavior base.render (Writer);
This gives a place to hook in an own textwriter where asp, net can render the html into. The constructor of the HTMLtextwriter class has a protected visibility. Passing an own htmltextwriter requires specializing the frameworks's HTMLtextwriter class. The constructor of the base class requires a StringWriter. a Wrapper Class to House Both Custom HtmlTextWriter and StringWriter:
Internal class myhtmlfilecreator {private stringwriter html; private myhtmltextwriter htmlwriter;
// override the HtmlTextWriter to reach the constructor // the constructor in the base class is protected class MyHtmlTextWriter: HtmlTextWriter {internal MyHtmlTextWriter (TextWriter tw): base (tw) {}} // publish the HTMLwriter internal HtmlTextWriter RenderHere {get {return Htmlwriter;}}
// constructor initializes stringwriter and htmlwriter based on that // initialize Url internal MyHtmlFileCreator () {html = new StringWriter (); htmlWriter = new MyHtmlTextWriter (html); newUrl = Context.Request.Url.AbsolutePath.ToString (); newUrl = Newurl.Replace (". aspx", ". htm");
internal void WriteHTMLFile (string virtualFileName) {// Stringreader reads output rendered by asp.net // Stringwriter writes html output file StringReader sr = new StringReader (html.ToString ()); StringWriter sw = new StringWriter ();
// read from input string htmlline = sr.readline (); while (htmlline! = Null) {// filter out asp.net specific tags if (! ((Htmlline.indexof ("