Problem trying to print a DataGrid

G

Guest

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_DataBinding) in Page1.aspx
to set DataSource

toPrint.EnableViewState = false;
toPrint.AutoGenerateColumns = false;

toPrint.CssClass = origin.CssClass;
toPrint.HeaderStyle.CssClass = origin.HeaderStyle.CssClass;
toPrint.AlternatingItemStyle.CssClass = origin.AlternatingItemStyle.CssClass;

foreach (DataGridColumn dgc in origin.Columns)
{
toPrint.Columns.Add(dgc); // ¡Also works with Templated Columns!
}
toPrint.DataSource = origin.DataSource; // Set the same DataSource.
toPrint.DataBind();

pg.EnableViewState = false;
pg.Controls.Add (frm);
frm.Attributes.Add ("runat", "server");
frm.Controls.Add (toPrint);
pg.DesignerInitialize();
pg.RenderControl(htmlWrite);

Page.RegisterStartupScript("iwc_toprint", "<div id='divPrint'
style='VISIBILITY: hidden; OVERFLOW: hidden; HEIGHT: 1px'>"
+ stringWrite.ToString() + "</div><script
language=javascript>CallPrint('divPrint');</script>");

strHTML.WriteLine("<script language=javascript>");
strHTML.WriteLine("function CallPrint(strid)");
strHTML.WriteLine("{");
strHTML.WriteLine("var prtContent = document.getElementById(strid);");
strHTML.WriteLine("var WinPrint = window.open('print.aspx','','');");
strHTML.WriteLine("WinPrint.focus();");
strHTML.WriteLine("}");
strHTML.WriteLine("</script>");

Page.RegisterClientScriptBlock("iwc_printfun", strHTML.ToString());


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="javascript">
function copy()
{
var prtContent = opener.document.getElementById('divPrint');
var dest = document.getElementById('res')

dest.innerHTML = prtContent.innerHTML;
// 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.Page.LoadPageStateFromPersistenceMedium()
System.Web.UI.Page.LoadPageViewState()
System.Web.UI.Page.ProcessRequestMain()

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top