| 
 | 1 | +---  | 
 | 2 | +title: Use Telerik Report Viewer in ASP.NET AJAX Applications  | 
 | 3 | +description: "Guide to embed Telerik Report Viewer in AJAX enabled ASP.NET Web Forms applications."  | 
 | 4 | +type: how-to  | 
 | 5 | +page_title: Use Telerik Report Viewer in ASP.NET AJAX Applications  | 
 | 6 | +slug: integration-use-telerik-report-viewer-in-aspnet-ajax-applications  | 
 | 7 | +tags: telerik, report, viewer, aspnet, ajax, web forms  | 
 | 8 | +ticketid: 1683007  | 
 | 9 | +res_type: kb  | 
 | 10 | +---  | 
 | 11 | + | 
 | 12 | +## Description  | 
 | 13 | + | 
 | 14 | +Telerik Report Viewer disappear after a Post Back while AJAX is enabled in the ASP.NET AJAX Web Forms application.  | 
 | 15 | + | 
 | 16 | +Unlike the Telerik UI for ASP.NET AJAX components, the Telerik Reporting for ASP.NET Web Forms does not support AJAX (`asp:UpdatePanel`, `telerik:RadAjaxManager`, `telerik:RadAjaxPanel`) out of the box.   | 
 | 17 | + | 
 | 18 | +The Report Viewer's initializer scripts are called inside `window.onload` event and work when the document first loads, however, an AJAX Post Back does not trigger this event, thus the Telerik Report Viewer application does not get initialized.  | 
 | 19 | + | 
 | 20 | +The functions that will only trigger on the initial load of the page but not on subsequent AJAX requests:  | 
 | 21 | + | 
 | 22 | +```javascript  | 
 | 23 | +window.onload = function () { }  | 
 | 24 | +jQuery(function () { });  | 
 | 25 | +$(document).ready(function () { });  | 
 | 26 | +```  | 
 | 27 | + | 
 | 28 | + | 
 | 29 | +## Solution  | 
 | 30 | + | 
 | 31 | +The solution would be to call the initializer scripts inside the [Sys.Application.load event](https://learn.microsoft.com/en-us/previous-versions/bb383829(v=vs.100)?redirectedfrom=MSDN) that will trigger upon AJAX requests.  | 
 | 32 | + | 
 | 33 | +To do that, attach the `PreRender` event to the Report Viewer which will trigger on every request (see [ASP.NET Page Life Cycle](https://learn.microsoft.com/en-us/previous-versions/aspnet/ms178472(v=vs.100))).  | 
 | 34 | + | 
 | 35 | +````ASP.NET  | 
 | 36 | +<telerik:ReportViewer ID="ReportViewer1" runat="server" OnPreRender="ReportViewer1_PreRender">  | 
 | 37 | +</telerik:ReportViewer>  | 
 | 38 | +````  | 
 | 39 | + | 
 | 40 | +In the event handler, you can check if the request is an AJAX PostBack (IsInAsyncPostBack), then register a Startup Script that will initialize the Report Viewer after the AJAX Post Back is complete.  | 
 | 41 | + | 
 | 42 | +>The `InitializeReportViewer()` method parses the Report Viewer's HTML DOM to find the initializer scripts generated for it then call those scripts inside the `Sys.Application.load` event.  | 
 | 43 | +
  | 
 | 44 | +````C#  | 
 | 45 | +protected void ReportViewer1_PreRender(object sender, EventArgs e)  | 
 | 46 | +{  | 
 | 47 | +    // Access the Application's ScriptManager  | 
 | 48 | +    ScriptManager sm = RadScriptManager.GetCurrent(Page);  | 
 | 49 | + | 
 | 50 | +    // Check if the request is an AJAX Post Back  | 
 | 51 | +    if (sm.IsInAsyncPostBack)  | 
 | 52 | +    {  | 
 | 53 | +        // Call a custom method  | 
 | 54 | +        InitializeReportViewer((ReportViewer)sender);  | 
 | 55 | +    }  | 
 | 56 | +}  | 
 | 57 | + | 
 | 58 | +protected void InitializeReportViewer(ReportViewer reportViewer)  | 
 | 59 | +{  | 
 | 60 | +    // Search pattern for regex  | 
 | 61 | +    string pattern = string.Format(@"syncReportViewerState\([^)]*\);|jQuery\('#{0}'\)\.telerik_ReportViewer\([^)]*\);", reportViewer.ID);  | 
 | 62 | + | 
 | 63 | +    // find the two functions containing all the params from the generated HTML string of reportViewer.ToString()  | 
 | 64 | +    MatchCollection matches = Regex.Matches(reportViewer.ToString(), pattern);  | 
 | 65 | + | 
 | 66 | +    // Concatenate both scripts  | 
 | 67 | +    string reportingScripts = string.Empty;  | 
 | 68 | +    foreach (Match match in matches)  | 
 | 69 | +    {  | 
 | 70 | +        reportingScripts += match.Value;  | 
 | 71 | +    }  | 
 | 72 | + | 
 | 73 | +    if (!string.IsNullOrEmpty(reportingScripts))  | 
 | 74 | +    {  | 
 | 75 | +        string startupScript = string.Format(@"  | 
 | 76 | +            function pageLoadHandler() {{   | 
 | 77 | +                {0}  | 
 | 78 | +                Sys.Application.remove_load(pageLoadHandler);   | 
 | 79 | +            }}   | 
 | 80 | +            Sys.Application.add_load(pageLoadHandler);", reportingScripts);  | 
 | 81 | + | 
 | 82 | +        // Register a Startup Script that will be executed after the AJAX Post Back  | 
 | 83 | +        ScriptManager.RegisterStartupScript(Page, Page.GetType(), Guid.NewGuid().ToString(), startupScript, true);  | 
 | 84 | +    }  | 
 | 85 | +}  | 
 | 86 | +````  | 
 | 87 | +````VB  | 
 | 88 | +Protected Sub ReportViewer1_PreRender(ByVal sender As Object, ByVal e As EventArgs)  | 
 | 89 | +    Dim sm As ScriptManager = RadScriptManager.GetCurrent(Page)  | 
 | 90 | + | 
 | 91 | +    If sm.IsInAsyncPostBack Then  | 
 | 92 | +        InitializeReportViewer(CType(sender, ReportViewer))  | 
 | 93 | +    End If  | 
 | 94 | +End Sub  | 
 | 95 | + | 
 | 96 | +Protected Sub InitializeReportViewer(ByVal reportViewer As ReportViewer)  | 
 | 97 | +    Dim pattern As String = String.Format("syncReportViewerState\([^)]*\);|jQuery\('#{0}'\)\.telerik_ReportViewer\([^)]*\);", reportViewer.ID)  | 
 | 98 | +    Dim matches As MatchCollection = Regex.Matches(reportViewer.ToString(), pattern)  | 
 | 99 | +    Dim reportingScripts As String = String.Empty  | 
 | 100 | + | 
 | 101 | +    For Each match As Match In matches  | 
 | 102 | +        reportingScripts += match.Value  | 
 | 103 | +    Next  | 
 | 104 | + | 
 | 105 | +    If Not String.IsNullOrEmpty(reportingScripts) Then  | 
 | 106 | +        Dim startupScript As String = String.Format("  | 
 | 107 | +        function pageLoadHandler() {{   | 
 | 108 | +            {0}  | 
 | 109 | +            Sys.Application.remove_load(pageLoadHandler);   | 
 | 110 | +        }}   | 
 | 111 | +        Sys.Application.add_load(pageLoadHandler);", reportingScripts)  | 
 | 112 | +        ScriptManager.RegisterStartupScript(Page, Page.[GetType](), Guid.NewGuid().ToString(), startupScript, True)  | 
 | 113 | +    End If  | 
 | 114 | +End Sub  | 
 | 115 | +````  | 
0 commit comments