The code I wrote will allow you to intercept whatever HTML the server outputs to the browser and filter it via REGEX expressions returning whatever you would like. It works via the page.response.filter property.
You can add the filter at any point (before pre-render) in your server-side code. This allows you to selectively apply filters based on server side code and to insert server-side parameters.
The HtmlFilter is now available on CodePlex.
Refer to the CodePlex project for future updates and usage examples.
- Add the HtmlFilter.vb file to the App_Code\VBCode folder
- See here for how to configure separate VBCode and CSCode folders using CodeSubdirectories in the web.config
- In your UserControl or Page CodeBehind, add the following code in the Page_Load event handler
- For VB
Dim FilterReplaceValues As New Specialized.NameValueCollection FilterReplaceValues.Add("Your Regex Expression", "Your Replacement Expression") HtmlFilter.SetHtmlFilter(Page, FilterReplaceValues, 10000)
- For C#
Specialized.NameValueCollection FilterReplaceValues = new Specialized.NameValueCollection(); FilterReplaceValues.Add("Your Regex Expression", "Your Replacement Expression"); HtmlFilter.SetHtmlFilter(Page, FilterReplaceValues, 10000);
- The number 10000 in the code above should be at least the length of any replaceable string.
- This is used in the stream to reduce buffer size.
- Example: If you will be replacing strings up to 20 characters, you would put 20 or more there.
- Fill in the appropriate Regex and Replacement expressions according to your needs.
- You may add multiple FilterReplaceValues to the collection. It will parse each one in order.
- Examples (VB)
- Remove all UserControl prepended Identifier strings from Form elements, for use in a custom cross-page post scenario.
"(name|id)=""[^""]*" & Me.ID & "[\$_]", "$1="""
- SharePoint: Remove the Edit and Export Web Part Edit items from the Edit Menu
"(?s:<ie:menuitem type=""separator"" /><ie:menuitem title(?!.*separator.*id=""MSOMenu_Edit"").*MSOMenu_Edit.*Export\.\.\.(\r|\n|\s)*</ie:menuitem>)", ""
- For help creating your own expressions, use some of these tools