Hello,
I founded some examples of WebControl printing in internet and tryed to have
them working for a DataGrid, something not very complicated.
I have a page, Page1.aspx with a DataGrid (paginated) that i want to get
alone in page Print.aspx not paginated. Both pages will have the same CSS
file so they will look identical except of the paginating.
There is a Button (Print) in Page1.aspx with this code:
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter( stringWrite);
Page pg = new Page();
HtmlForm frm = new HtmlForm();
StringWriter strHTML = new StringWriter();
DataGrid toPrint = new DataGrid();
origin.DataBind (); //Fires an event (origin_DataBin ding) in Page1.aspx
to set DataSource
toPrint.EnableV iewState = false;
toPrint.AutoGen erateColumns = false;
toPrint.CssClas s = origin.CssClass ;
toPrint.HeaderS tyle.CssClass = origin.HeaderSt yle.CssClass;
toPrint.Alterna tingItemStyle.C ssClass = origin.Alternat ingItemStyle.Cs sClass;
foreach (DataGridColumn dgc in origin.Columns)
{
toPrint.Columns .Add(dgc); // ¡Also works with Templated Columns!
}
toPrint.DataSou rce = origin.DataSour ce; // Set the same DataSource.
toPrint.DataBin d();
pg.EnableViewSt ate = false;
pg.Controls.Add (frm);
frm.Attributes. Add ("runat", "server");
frm.Controls.Ad d (toPrint);
pg.DesignerInit ialize();
pg.RenderContro l(htmlWrite);
Page.RegisterSt artupScript("iw c_toprint", "<div id='divPrint'
style='VISIBILI TY: hidden; OVERFLOW: hidden; HEIGHT: 1px'>"
+ stringWrite.ToS tring() + "</div><script
language=javasc ript>CallPrint( 'divPrint');</script>");
strHTML.WriteLi ne("<script language=javasc ript>");
strHTML.WriteLi ne("function CallPrint(strid )");
strHTML.WriteLi ne("{");
strHTML.WriteLi ne("var prtContent = document.getEle mentById(strid) ;");
strHTML.WriteLi ne("var WinPrint = window.open('pr int.aspx','','' );");
strHTML.WriteLi ne("WinPrint.fo cus();");
strHTML.WriteLi ne("}");
strHTML.WriteLi ne("</script>");
Page.RegisterCl ientScriptBlock ("iwc_printfun" , strHTML.ToStrin g());
All of this create a hidden DIV called divPrint with the HTML for the
DataGrid toPrint, then open a new window to print.aspx with the following
code.
<HEAD>
<script language="javas cript">
function copy()
{
var prtContent = opener.document .getElementById ('divPrint');
var dest = document.getEle mentById('res')
dest.innerHTML = prtContent.inne rHTML;
// opener.history. back(1);
print();
}
</script>
</HEAD>
<body onload="copiar( );">
<br>
<div id='res'>
</div>
</body>
All works but i receive an error in Page1.aspx when i try to continue
working with it (print contents close print.aspx and do something else to
send a POST).
The error message is:
[HttpException (0x80004005): The View State is invalid for this page and
might be corrupted.]
System.Web.UI.P age.LoadPageSta teFromPersisten ceMedium()
System.Web.UI.P age.LoadPageVie wState()
System.Web.UI.P age.ProcessRequ estMain()
A workaround (i dont like) is to uncomment this line in print.aspx.
// opener.history. back(1);
But i'd like to find a better solution.
I think that, for example, doing this:
foreach (DataGridColumn dgc in origin.Columns)
{
toPrint.Columns .Add(dgc); // ¡Also works with Templated Columns!
}
is not very safe, hehe...
Thanks a lot for your help.
Best regards,
Paco Ferre