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

[Solved] How can I catch "invalid xhtml" exceptions when exporting to PDF?

4 Answers 160 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 08 May 2009, 02:41 PM
I have code to export the grid to pdf, which works fine as long as all the text is well-formed xhtml. In this application, the xhtml is user-supplied, and it's not always well-formed.

I added a try-catch around the export call, but it doesn't seem to catch the exception. When I trace the execution in the debugger, the error doesn't occur until after the code is past the try-catch block.

            try
            {
                switch (exportType)
                {
                    case 1:
                        grid.ExportSettings.ExportOnlyData = true;
                        grid.MasterTableView.ExportToExcel();
                        break;
                    case 2:
                        grid.MasterTableView.ExportToPdf();
                        break;
                    case 3:
                        grid.MasterTableView.ExportToWord();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                string errorMessage = "Data export failed. ";
                //display message, etc.
            }

4 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 12 May 2009, 07:38 AM
Hi Paul,

You can't catch the exception in this way because the xhtml of the grid is parsed when the page starts rendering, while grid's export methods just attach a render method delegate to the page. This means that the grid's ExportToPdf method is detached from the exporting itself and hence the exception cannot be caught through the approach you have adopted. 

Try catching the exception on a global level in the global.asax error handler.

I hope this helps.

Best Regards,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Paul
Top achievements
Rank 1
answered on 12 May 2009, 03:07 PM
Thank you. I am able to catch the error in either the global Application_Error() or the page.OnError. I create an error message string but I can't get it displayed to the user. The page is not redrawn when the user clicks on the Export Data button, so the error message doesn't appear.

Any suggestions for how to force the message text to appear? Can I catch this error client-side, so I could directly display an alert? Can I use a RadWindow from the server side, where I'm catching the error?
0
Tsvetoslav
Telerik team
answered on 15 May 2009, 09:06 AM
Hi Paul,

Please, review the attached sample for one way to display an error message when the xhtml of the grid is invalid and let us know if you need further directions.

Best regards,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Paul
Top achievements
Rank 1
answered on 15 May 2009, 04:38 PM
Thanks very much! That works beautifully. I ended up using a Session variable to set the error message, instead of a query string. When the page was ajaxified with the RadAjaxManager, the query string remained set until navigating away from that page . I clear the session variable once I've displayed the error message, so it's only displayed once even if the user continues working on that page. I like the RadAjaxManager.alert functionality for displaying the error, and wouldn't have noticed that option without your sample.
Tags
Grid
Asked by
Paul
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Paul
Top achievements
Rank 1
Share this question
or