Sunday, October 6, 2013

Replace HtmlTextWriter before render

If we want to change HTML of control before rendering you can use the following code:
protected override void Render(HtmlTextWriter writer)
{

MemoryStream stream = new MemoryStream();
StreamWriter memWriter = new StreamWriter(stream);
HtmlTextWriter myWriter = new HtmlTextWriter(memWriter);
base.Render(myWriter);
myWriter.Flush();
stream.Position = 0;
using (StreamReader sr = new StreamReader(stream))
{
string renderedHtml = sr.ReadToEnd();
renderedHtml = renderedHtml.Replace("It looks like your browser does not have JavaScript enabled. Please turn on JavaScript and try again.", "");
writer.Write(renderedHtml);
writer.Close();
myWriter.Close();
}
//base.Render(writer);
}

No comments: