Problem trying to print a DataGrid

Discussion in 'ASP .Net' started by =?Utf-8?B?UGFjbyBGZXJyZQ==?=, Jun 22, 2005.

  1. 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
     
    =?Utf-8?B?UGFjbyBGZXJyZQ==?=, Jun 22, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    297
    Alex Martelli
    Mar 19, 2007
  2. keto
    Replies:
    0
    Views:
    1,055
  3. Lew
    Replies:
    0
    Views:
    651
  4. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    408
    David Cournapeau
    Dec 30, 2008
  5. Scott Navarre

    Trying to print() one frame from another

    Scott Navarre, Jan 22, 2004, in forum: Javascript
    Replies:
    4
    Views:
    201
    DJ WIce
    Jan 30, 2004
Loading...

Share This Page