Thursday, July 16, 2009

Setting Up Print view when using a Master Page

Lets say you are using Master Pages in you ASP.Net Web Site or Application and want to set up a print view for your users. Your master page has top navigation / side navigation etc which you do not want to appear in the print view. A simple solution is to pass a print query string in the URL and change your master page to a one suitable for print view. You will need to do this in the Page_PreInit event.

Code Sample:
void Page_PreInit(Object sender, EventArgs e)
{
if (Request.QueryString["print"] == 1)
{
MasterPageFile = "~/MasterPage/Print.Master";
}
}

[ BTW, the above code syntax highlighting is by http://alexgorbatchev.com/wiki/SyntaxHighlighter. Absolutely love this! Thank you, Alex. ]

Happy Coding!

Ravi







Reblog this post [with Zemanta]