Print from dynamic RadWindow

11 posts, 0 answers
  1. Jerry
    Jerry avatar
    4 posts
    Member since:
    Jul 2010

    Posted 11 Aug 2011 Link to this post

    We're trying to use a RadWindow for printing some information entered by the user:

    Here are the basic steps we're using in code behind:
    • New up a RadWindow

    RadWindow window = new RadWindow();
    window.ID = "windowShowPrintDialog";
    //window.NavigateUrl = "PrintAdHoc.aspx";
    window.VisibleOnPageLoad = true;
    window.Modal = true;
    window.Width = 200;
    window.Height = 300;

    • Use LiteralControl to add info and JavaScript to window

    string jsPrint = null;
    using (StreamReader sr = new StreamReader(Server.MapPath("PrintWindowJavaScript.txt")))
    {
        jsPrint = sr.ReadToEnd();
    }
     
    window.ContentContainer.Controls.Add(new LiteralControl(jsPrint));
     
    ...
     
     
     
    window.ContentContainer.Controls.Add(new LiteralControl(name));
    window.ContentContainer.Controls.Add(new LiteralControl(startdate));
    window.ContentContainer.Controls.Add(new LiteralControl(enddate));
    window.ContentContainer.Controls.Add(new LiteralControl("<button onclick=\"printWin(); return false\">print content</button><br />"));


    • PrintWindowJavaScript.txt is:

    <script type="text/javascript">
    function GetRadWindow()
    {
        var oWindow = null;
        if (window.radWindow) oWindow = window.radWindow;
        else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
        return oWindow;
    }
     
     
     function printWin()
                {
                    var oWnd = GetRadWindow();
                    var content = oWnd.GetContentFrame().contentWindow;
                    var printDocument = content.document;
                    if (document.all)
                    {
                        printDocument.execCommand( "Print");
                    }
                    else
                    {
                        content.print();
                    }
     
                }  
     
    </script>
    • Show window by adding it to the for controls

    this.Form1.Controls.Add(window);


    The RadWindow is displayed along with the print button.  When the print button is clicked, we get a java script error:

    Microsoft JScript runtime error: Object required

    at (see bold text):

    function GetRadWindow()
    {
        var oWindow = null;
        if (window.radWindow) oWindow = window.radWindow;
        else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
        return oWindow;
    }

    Is it possible to print and/or run javascript code from a RadWindow created like this?

    Thanks,

    Jerry



  2. Kevin
    Kevin avatar
    360 posts
    Member since:
    Jul 2012

    Posted 12 Aug 2011 Link to this post

    Hello Jerry,

    I'm not so sure if what you're doing is possible, when using the ContentTemplate of the RadWindow. The reason being is that when you use the ContentTemplate, it doesn't use an iFrame to render the content because it's not loading another page. The RadWindow just becomes a wrapper for the content, so the GetRadWindow function doesn't really work in this case because it's for when you don't use the ContentTemplate.

    The only way I see it working, is that you have to setup a Print css style to hide everything else on screen from printing and make the content of the RadWindow the only thing that is visible when printing.

    I hope that helps.
  3. Jerry
    Jerry avatar
    4 posts
    Member since:
    Jul 2010

    Posted 12 Aug 2011 Link to this post

    So, printing from a RadWindow as described in the thread below can be used when using the NavigateURL?

    http://www.telerik.com/community/forums/aspnet-ajax/window/printing-radwindow-prints-parent-page-in-background.aspx

    Jerry

  4. Marin Bratanov
    Admin
    Marin Bratanov avatar
    4614 posts

    Posted 16 Aug 2011 Link to this post

    Hi Jerry,

    The approach you have found (I assume this KB article) is for the scenario when the NavigateUrl property is used, as it creates a separate document object, which you can print independently of the main page. Besides, at the point when this KB was created the RadWindow had not yet offered the ContentTemplate functionality and thus the functions employed will not work directly in your scenario. I believe the following KB article will help you in your endeavor: http://www.telerik.com/support/kb/aspnet-ajax/tooltip/print-radtooltip-content.aspx. It is designed for the RadToolTip, yet the approach can be adopted for the RadWindow as well - just add a wrapper, access it via JavaScript and pass the HTML content in the same manner to a new browser window.


    Regards,
    Marin
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

  5. Curley
    Curley avatar
    15 posts
    Member since:
    Mar 2011

    Posted 07 Sep 2011 Link to this post

    I'm having trouble with this as well. I'm trying to print content from a dynamic RadWindow that has content wrapped  within Content Template. Any help would be more than grateful.
  6. Marin Bratanov
    Admin
    Marin Bratanov avatar
    4614 posts

    Posted 08 Sep 2011 Link to this post

    Hi Curley,

    Have you tried the approach described in the second KB article I linked (http://www.telerik.com/support/kb/aspnet-ajax/tooltip/print-radtooltip-content.aspx)? If so what is the difficulty you are experiencing?

    I tested the approach and it worked as expected on my end: http://screencast.com/t/OSRGOKVg1SP. I have also attached my test page to this thread. It is based on the KB article linked above.


    Regards,
    Marin
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  7. Curley
    Curley avatar
    15 posts
    Member since:
    Mar 2011

    Posted 08 Sep 2011 Link to this post

    Wow thanks that did the trick, but I still have one issue. Can the objects inside of the content template print WITHOUT making an addtional html popup? Or can I somehow make the printable html pop hidden?
  8. Curley
    Curley avatar
    15 posts
    Member since:
    Mar 2011

    Posted 08 Sep 2011 Link to this post

    The reason that I ask to print the content template objects without an additional HTML popup is because I'm trying to print the content template objects from a RadWindow. The RadWindow opens with an embedded Crystal Report. I tried using the technique that allows you to print the RadWindow's frame but it falis on the following:

    var content = oWnd.GetContentFrame().contentWindow;

    The RadWindow opens without a iFrame so the java script can't find a frame to print.

    This technique of printing the content template works except I don't need the additonal popup. I sincerely appreciate all of your help. This is the last little snag that I have with building this RadWindow.
  9. Marin Bratanov
    Admin
    Marin Bratanov avatar
    4614 posts

    Posted 13 Sep 2011 Link to this post

    Hi Curley,

    Your support ticket on the matter has been answered and I will post my reply here as well for other people who may have a similar inquiry.:




    The concept is that you need a window object where you can put only the HTML you wish to print, so that you don't print the entire page. You can achieve this by using either a new browser window, or a new iframe. The code for a hidden iframe is not that different from the code for a new window, but its cross-browser compatibility is lower, as iframes tend to behave very differently between browsers. As for controlling a new browser window - this is hardly possible, as the state in which new windows appear tends do be quite inconsistent between browsers and depends on previously closed popups and other factors. Here is an example of printing from a hidden iframe:

    ar content = $get("contentHolder").innerHTML; 
       
    var theIframe = $get("ifmcontentstoprint"); 
    var iframeWindow = theIframe.contentWindow; 
    iframeWindow.document.open(); 
    iframeWindow.document.write(content); 
    iframeWindow.document.close(); 
    iframeWindow.focus(); 
    iframeWindow.print();

    where ifmcontentstoprint is an iframe declared in the page:

    <iframe id="ifmcontentstoprint" style="display: none;"></iframe>
    You can also try some jQuery solutions from the net:
    http://www.google.com/search?q=jquery+print


    All the best,
    Marin
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  10. Curley
    Curley avatar
    15 posts
    Member since:
    Mar 2011

    Posted 13 Sep 2011 Link to this post

    I actually tried using the iFrames as described, but it still printed the whole page and not just the Rad Window. It's actually very weird because I removed the display:none attribute so I can see the actual actual HTML that's written to the Rad Window but it still printed the whole page. This happens in both IE 8 and IE9.
  11. Marin Bratanov
    Admin
    Marin Bratanov avatar
    4614 posts

    Posted 14 Sep 2011 Link to this post

    Hi Curley,

    I just answered your support ticket on the matter. In the future please post only one thread per issue as it would help us track its history and would speed up the response.




    This stems from the browser behavior when printing pages and iframes. Such browser inconsistencies are the reason why the KB article uses a separate browser window.

    Nevertheless I researched this scenario and I am attaching a modified version of the code that seems to work correctly in the major browsers. I am also attaching the expected results from my machine (*.xps format, it is MS's XPS Document Writer, I use it to save the printed page in a file instead of actually printing it).

    Please note that our support system is focused on providing workarounds for bugs and controls' incompliances and we do not develop custom solutions, such as printing from iframes, which is considered general knowledge.


    Best wishes,
    Marin
    the Telerik team
    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal
Back to Top