Hire my services

If you would like to hire my services, you can now do so by visiting the following link:

Website Design Darlington

Article Statistics

ArticlesLatest Entry
2121-May-2008

Code Bank Statistics

CategoryTipsLatest Entry
Applications420-Feb-2008
Cache118-Apr-2007
Controls926-Jul-2007
CSS107-Sep-2007
Database428-Aug-2007
Dates128-Aug-2007
Email105-Mar-2007
Files123-Apr-2007
HTML101-Mar-2007
Images226-Mar-2007
Pages115-Oct-2007
Sessions122-Nov-2007
SQL Server1518-Mar-2008
Strings309-May-2007
Skip Navigation LinksHome > Articles > Use Response.Filter to intercept your HTML

Use Response.Filter to intercept your HTML

Summary: You can use Response.Filter to get a reference to the final HTML that will be sent to the clientSocialize it

Sometimes, we need to see the actual HTML that is going to be sent to our client once a page is requested. There are many uses for this and to demonstrate it's functionality, I'm going to show you how the Response.Filter class can be used to intercept the HTML and change some text before it is sent to the client.

First, let's create a page named "MyPage.aspx" and add the following:

If we run this page, we see our simple page displayed and the "Hello World!" message is displayed.

To intercept and get a reference to the HTML, we now need to create a class to inherit System.IO.Stream. So, create a new class in your App_Code folder, call it "ReplaceHTML" and add the following code:

You can see from the above code that when the class is created, the New method sets it's "Base" variable to the ResponseStream that we will soon send in from the page. The Write method then performs a Replace on the actual HTML and then we write it back out.

All we need to do now is tell the page to call this class when the page loads. This is where the Response.Filter method comes in and it's very easy to implement. Simply add this code to the "MyPage.aspx" page that we created earlier:

Now, when we run the page, our "Hello World!" text has been replaced just as we asked it to!

Obviously this is just a simple example, but this could easily be extended to incorporate Regular Expressions if anything more complicated is needed, but hopefully this provides a starting point.

Reader Comments

1. Hai 31/03/2007 06:40:16

2. Chris Davis 31/03/2007 09:35:22

3. PohEe.com 31/03/2007 10:57:35

4. Mark Smith 01/04/2007 01:53:01

5. DotNetUrls.com 01/04/2007 18:22:15

6. Jasmine 24/05/2007 12:24:39

7. Andrey Tagaew 23/10/2007 01:36:26

8. Ted 18/01/2008 04:16:20

Add your comments

Please keep your comments relevant to this page. Any inappropriate or purely promotional comments may be removed. Email addresses are never displayed but are required so you can confirm your comments.

Your Name:
 
Your Email:
 
Add your comments: