This is a migrated thread and some comments may be shown as answers.

Print from dynamic RadWindow

10 Answers 108 Views
Window
This is a migrated thread and some comments may be shown as answers.
Jerry
Top achievements
Rank 1
Jerry asked on 12 Aug 2011, 01:06 AM
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



10 Answers, 1 is accepted

Sort by
0
Kevin
Top achievements
Rank 2
answered on 12 Aug 2011, 01:04 PM
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.
0
Jerry
Top achievements
Rank 1
answered on 12 Aug 2011, 03:59 PM
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

0
Marin Bratanov
Telerik team
answered on 16 Aug 2011, 11:47 AM
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.

0
Curley
Top achievements
Rank 1
answered on 07 Sep 2011, 03:26 PM
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.
0
Marin Bratanov
Telerik team
answered on 08 Sep 2011, 04:13 PM
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 >>

0
Curley
Top achievements
Rank 1
answered on 08 Sep 2011, 05:43 PM
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?
0
Curley
Top achievements
Rank 1
answered on 08 Sep 2011, 07:39 PM
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.
0
Marin Bratanov
Telerik team
answered on 13 Sep 2011, 03:23 PM
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.
0
Curley
Top achievements
Rank 1
answered on 13 Sep 2011, 03:28 PM
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.
0
Marin Bratanov
Telerik team
answered on 14 Sep 2011, 09:50 AM
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
Tags
Window
Asked by
Jerry
Top achievements
Rank 1
Answers by
Kevin
Top achievements
Rank 2
Jerry
Top achievements
Rank 1
Marin Bratanov
Telerik team
Curley
Top achievements
Rank 1
Share this question
or