Best way to intercept control rendering
I have a control used in our CMS and we don't have the source code for it, what I would like to do is ch开发者_StackOverflowange the rendered output of this control.
Now, I could have a check in my base Page class that checks if the control is being used on the page and then change the html that needs to be altered, but that seems a bit excessive for just 1 usage.
So is there any other way of changing the behaviour of the control without the source code? I'm thinking not other than the way described above.
Thanks
Wrap it in a custom control:
public class MyCMSControl: CommercialCMSControl
{
protected override void Render(HtmlTextWriter writer)
{
StringBuilder stringBuilder = new StringBuilder();
StringWriter stringWriter = new StringWriter(stringBuilder );
using (HtmlTextWriter myWriter = new HtmlTextWriter(stringWriter ))
{
base.Render(myWriter);
string newOutput;
// the original output is in stringBuilder, do whatever you want, and
// put it in newOutput
writer.Write(newOutput);
}
}
}
If you want to be able to manipulate the output in code specific to the page, add an event, something like:
public delegate void OnRenderHandler(object sender, string originalOutput, HtmlTextWriter writer)
public OnRenderHandler OnRender;
...
/// before writer.Write above...
if (OnRender!=null) {
OnRender(this,stringBuilder.ToString(),writer);
}
To make your custom version available in the designer, you need something in web.config
<pages>
<controls>
<add namespace="My.Control.Namespace" assembly="My.Control.Assembly" tagPrefix="MyControlsPrefix"/>
</controls>
</pages>
精彩评论