Skip navigation

Cache Pages With Server.Execute

Request a page, then store the returned text in a TextWriter object.

asp:Feature

TECHNOLOGIES: Server.Execute | TextWriter | Caching

LANGUAGES: VB .NET

 

Cache Pages With Server.Execute

Request a page, then store the returned text in a TextWriter object.

 

By Dino Esposito

 

The Server.Execute method executes a request to another page and lets you decide how to use the returned text. By default, the output is appended to the response stream of the caller page. By using an overload of Execute, however, you can cache all the generated output in a TextWriter object, such as System.IO.StringWriter:

 

Dim writer As StringWriter = New StringWriter()

Server.Execute("childpage.aspx", writer)

 

This behavior of the Execute method is like calling a language function and storing its return value in some local variable for further processing. The output of the child page is in no way required to be HTML. For instance, it instead can be XML. In this case, by combining the Execute method with the XML server control, you easily can set up a multibrowser presentation layer. You put this tag in the body of the page:

 

<asp:xml runat="server" id="presLayer" />

 

Finally, bind the XML server control dynamically to the XML contents generated by Execute and the XSLT file that is correct for the current browser:

 

Sub Page_Load()  

Dim writer As StringWriter = New StringWriter()

Server.Execute("childpage.aspx", writer)

presLayer.DocumentContent = writer.ToString()

presLayer.TransformSource = GetXsltForBrowser()

End Sub

 

This can really come in handy if you're trying to reuse as much of your old ASP-based applications as possible.

 

For more, see Dino's article Pass Values Between Web Pages.

 

Dino Esposito is a trainer and consultant who specializes in ASP.NET, ADO.NET, and XML. A member of Wintellect and co-founder of http://www.VB2TheMax.com, Dino wrote Building Web Solutions with ASP.NET and ADO.NET and the upcoming Programming ASP.NET, both from Microsoft Press. Write to him at mailto:[email protected].

 

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish