ASP.net dynamically generate MS Word document (3)

If you are following the previous two articles, you will see a “Word” document in your frond. However, you may find some glitches in this document and there is a hidden box on top of the document. This is because ASP.Net generates “__VIEWSTATE” hidden view to track each page status.

We don’t want this field to be displayed as this should be a final “Word” document without any attached strings. To get rid of this, we have to override Render method.

protected override void Render(HtmlTextWriter writer)
{
  StringBuilder sb = new StringBuilder();
  HtmlTextWriter htw = new HtmlTextWriter(new StringWriter(sb));
  base.Render(htw);
  sb = sb.Replace(""
        ,string.Empty);
  writer.Write(sb.ToString());
}

You should have a complete “Word” document right now.