By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,854 Members | 833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,854 IT Pros & Developers. It's quick & easy.

Problem trying to print a DataGrid

P: n/a
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.LoadPageStateFromPersistenceMed ium()
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

Nov 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.