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

Executing javascript after disabling AJAX

1 Answer 56 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Juan
Top achievements
Rank 1
Juan asked on 26 Nov 2012, 12:47 PM
Hey Guys,

I've followed one of the forum topics on here in respect of disabling AJAX for a specific postback. Basically I have a button on a page that creates a PDF file. Because the routine is modifying the response and adding response headers, AJAX failed to work. So this is what I've done:

ASPX Page:

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" EnableAJAX="true" UpdatePanelsRenderMode="Inline" ClientEvents-OnRequestStart="onRequestStart">
<%-- AJAX EVENTS GOES HERE --%>
</telerik:RadAjaxManager>

The ClientEvents-OnRequestStart javascript script looks like this:

function onRequestStart(ajaxManager, eventArgs) {
            if (eventArgs.EventTarget == "ctl00$ContentPlaceHolder1$btnMainExportToPDF") {
                eventArgs.set_enableAjax(false);
                document.getElementById("<%= btnMainExportToPDF.ClientID %>").style.display = "none";
                document.getElementById("<%= imgExportLoader.ClientID %>").style.display = "";
            }
        }

On the server side I have this:

protected void btnMainExportToPDF_Click(object sender, EventArgs e)
    {
        string transDateStart = "";
        string transDateEnd = "";
        if (dtTransDateStart.SelectedDate.HasValue && dtTransDateEnd.SelectedDate.HasValue)
        {
            transDateStart = string.Format("{0}-{1}-{2}", dtTransDateStart.SelectedDate.Value.Year.ToString(), dtTransDateStart.SelectedDate.Value.Month.ToString().PadLeft(2, '0'), dtTransDateStart.SelectedDate.Value.Day.ToString().PadLeft(2, '0'));
            transDateEnd = string.Format("{0}-{1}-{2}", dtTransDateEnd.SelectedDate.Value.AddDays(1).Year.ToString(), dtTransDateEnd.SelectedDate.Value.AddDays(1).Month.ToString().PadLeft(2, '0'), dtTransDateEnd.SelectedDate.Value.AddDays(1).Day.ToString().PadLeft(2, '0'));
        }
 
        string storeID = hfActiveSelectedStoreID.Value;
        string pageRequestURL = string.Format("ReportsPrint-GlobalSpendHistory.aspx?TS={0}&TE={1}&SI={2}", NecBase.Encryption(transDateStart), NecBase.Encryption(transDateEnd), NecBase.Encryption(storeID));
 
        string gridQuery = GetMainGridQuery(false);
        Session["mainGridQuery"] = NecBase.Encryption(gridQuery);
 
        //Also add to Session: Master Category, Sub Category, Products
        IList<RadComboBoxItem> selMasterCategories = cboMainCategories.CheckedItems;
        IList<RadComboBoxItem> selSubCategories = cboSubCategories.CheckedItems;
        //IList<RadComboBoxItem> selProducts = cboProducts.CheckedItems;
 
        Session["selMasterCats"] = GetStringArrayFromIList(selMasterCategories);
        Session["selSubCats"] = GetStringArrayFromIList(selSubCategories);
        //Session["selProducts"] = GetStringArrayFromIList(selProducts);
 
        StringWriter sWriter = new StringWriter();
        Server.Execute(pageRequestURL, sWriter);
 
        string htmlCodeToConvert = sWriter.GetStringBuilder().ToString();
 
        PdfConverter pdfConverter = new PdfConverter();
        pdfConverter.LicenseKey = ConfigurationManager.AppSettings["EvoPdfKey"].ToString();
        pdfConverter.HtmlViewerWidth = 793;
 
        pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4;
        pdfConverter.PdfDocumentOptions.PdfPageOrientation = PdfPageOrientation.Portrait;
        pdfConverter.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel.Normal;
        pdfConverter.PdfDocumentOptions.ShowHeader = false;
        pdfConverter.PdfDocumentOptions.ShowFooter = false;
        pdfConverter.PdfDocumentOptions.FitWidth = true;
 
        pdfConverter.PdfDocumentOptions.EmbedFonts = true;
        pdfConverter.PdfDocumentOptions.LiveUrlsEnabled = true;
 
        pdfConverter.JavaScriptEnabled = true;
        pdfConverter.PdfDocumentOptions.JpegCompressionEnabled = true;
 
        byte[] pdfBytes = pdfConverter.GetPdfBytesFromHtmlString(htmlCodeToConvert.ToString());
 
        string fileName = string.Format("SpendHistoryReport_{0}{1}{2}", DateTime.Today.Date.Day.ToString(), DateTime.Today.Date.Month.ToString(), DateTime.Today.Date.Year.ToString());
 
        HttpResponse response = HttpContext.Current.Response;
        response.Clear();
        response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.pdf; size={1}", fileName, pdfBytes.Length.ToString()));
        response.BinaryWrite(pdfBytes);
        response.End();
    }

My question now is, after the server code has executed, I need to re-enable the controls which I just disabled using the Javascript script. I have tried using the ScriptManager.RegisterStartupScript but the javascript never gets called. How can I re-enable the controls after the entire routine has finished executing?

The javascript script I'm using to re-enable the controls looks like this:

function reEnableControl() {
            alert('test');
            document.getElementById("<%= imgExportLoader.ClientID %>").style.display = "none";
            document.getElementById("<%= btnMainExportToPDF.ClientID %>").style.display = "";
        }

The alert box is never shown either. I need to re-enable the controls so that the user can continue selecting other filters or re-export the document if they choose to do so. Since the code modifies the response headers, I cannot ajaxify the button control and use a loading panel instead. Any ideas or thoughts?

The reason why I'm hiding the controls is so that the user is aware that there is activity at that point in time after clicking the button.

1 Answer, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 29 Nov 2012, 09:37 AM
Hi Juan,

Form the information provided I did not get a clear idea who is calling reEnableControl method. Could you please try out the following and tell me if this works out for you?
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" EnableAJAX="true" UpdatePanelsRenderMode="Inline"  ClientEvents-OnResponseEnd="reEnableControl"  ClientEvents-OnRequestStart="onRequestStart">
<%-- AJAX EVENTS GOES HERE --%>
</telerik:RadAjaxManager>
Additionally you should note that if your controls are nested in other controls and you hide the parent controls server-side the child controls would not be accessible client-side.

Regards,
Angel Petrov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Ajax
Asked by
Juan
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Share this question
or