B
Brian Paul
When a user clicks on a linkbutton on a page, i would like to render a printer-friendly version of the asp.net page and download it as an html attachment to the browser. The code below works great, with a few exceptions:
1) IE displays the FIle Download dialog box twice. (You have to click the Open button twice)
2) the encoding of the page is set to Western European (Windows) versus UTF-8.
#1 above is just a pain.
#2 above results in some wingding fonts to be displayed incorrectly in the page.
Anyone have suggestions on how to fix these problems or see any problems in the code? #2 is our highest priority.
CODE:
// this code is implemented in the Render method of the page
// when a user click the button, it sets renderAsAttachement = true, and adds the desired controls to be rendered
// to the propControlContainer Controls collection
protected override void Render(HtmlTextWriter writer)
{
if (renderAsAttachement)
{
Response.Clear();
Response.ContentEncoding = Encoding.UTF8;
Response.Charset = "utf-8";
Response.AppendHeader("Content-Disposition", String.Format("attachment;Filename=\"{0}\"", "Document1.htm"));
Response.ContentType = "text/html";
writer.WriteFullBeginTag("html");
writer.WriteLine();
writer.WriteFullBeginTag("head");
writer.WriteLine();
writer.Write(Globals.ColorScheme); // this renders a style sheet that is used by the page
writer.WriteLine();
writer.WriteEndTag("head");
writer.WriteLine();
writer.WriteFullBeginTag("body style=\"padding:5pt;\"");
writer.WriteLine();
propControlContainer.RenderControl(writer); // this is a parent control that renders all the child controls (in it's control collection)
// that are to be included in the "printer-friendly" version of the page
writer.WriteEndTag("body");
writer.WriteLine();
writer.WriteEndTag("html");
writer.WriteLine();
Response.End();
}
else
{
base.Render(writer);
}
}
1) IE displays the FIle Download dialog box twice. (You have to click the Open button twice)
2) the encoding of the page is set to Western European (Windows) versus UTF-8.
#1 above is just a pain.
#2 above results in some wingding fonts to be displayed incorrectly in the page.
Anyone have suggestions on how to fix these problems or see any problems in the code? #2 is our highest priority.
CODE:
// this code is implemented in the Render method of the page
// when a user click the button, it sets renderAsAttachement = true, and adds the desired controls to be rendered
// to the propControlContainer Controls collection
protected override void Render(HtmlTextWriter writer)
{
if (renderAsAttachement)
{
Response.Clear();
Response.ContentEncoding = Encoding.UTF8;
Response.Charset = "utf-8";
Response.AppendHeader("Content-Disposition", String.Format("attachment;Filename=\"{0}\"", "Document1.htm"));
Response.ContentType = "text/html";
writer.WriteFullBeginTag("html");
writer.WriteLine();
writer.WriteFullBeginTag("head");
writer.WriteLine();
writer.Write(Globals.ColorScheme); // this renders a style sheet that is used by the page
writer.WriteLine();
writer.WriteEndTag("head");
writer.WriteLine();
writer.WriteFullBeginTag("body style=\"padding:5pt;\"");
writer.WriteLine();
propControlContainer.RenderControl(writer); // this is a parent control that renders all the child controls (in it's control collection)
// that are to be included in the "printer-friendly" version of the page
writer.WriteEndTag("body");
writer.WriteLine();
writer.WriteEndTag("html");
writer.WriteLine();
Response.End();
}
else
{
base.Render(writer);
}
}