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

Pdf Export on AjaxRequest

1 Answer 46 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Steven
Top achievements
Rank 1
Steven asked on 12 Jun 2015, 05:23 PM

I have a radgrid with an export button which I was able to exclude from using ajax via ajaxmanager. 
However I now require a prompt to the user to choose an option which affects the data that will be exported.
I use a custom rad prompt template to get a dropdownlist option and use a callback function in javascript which makes
an ajax request passing the parameter containing the dropdownlist option selected and then call my export function which no longer
works. Is there a good workaround so my export will still work after a prompt to the user?

 Thanks for the help.

 

protected void rg_ClientCalculators_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "ExportCalculator")
        {
            ViewState["CalculatorID"] = int.Parse(e.CommandArgument.ToString());
            RadWindowManager1.RadPrompt("Show all line items?", "callbackFn", 300, 300, null, "Calculator Export", " ");     
        }
    }
 
protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
    {
        if (e.Argument == "Yes")
            ExportCalculator(true);
        else if (e.Argument == "No")
            ExportCalculator(false);       
    }
 
 private void ExportCalculator(bool showAllLineItems)
    {
        Company UsersCompany = DAL.GetCompanyInfo(CurrentUser.UserID);
        var calculatorID = (int)ViewState["CalculatorID"];
        DataTable CalculatorClientInfo = DAL.ExportCalculatorClientInfo(calculatorID);
 
        ReportProcessor Processor = new ReportProcessor();
        InstanceReportSource ReportSource = new InstanceReportSource();
        ClientTrackerReports.CalculatorExport Report = new CalculatorExport();
        Report.ReportParameters["CalculatorID"].Value = calculatorID;
        Report.ReportParameters["CompanyName"].Value = UsersCompany.CompanyName;
        Report.ReportParameters["ClientName"].Value = CalculatorClientInfo.Rows[0]["ClientName"];
        Report.ReportParameters["AnotherVendor"].Value = CalculatorClientInfo.Rows[0]["AnotherVendor"];
        Report.ReportParameters["DateOfStatementProvided"].Value = CalculatorClientInfo.Rows[0]["DateOfStatementProvided"];
        Report.ReportParameters["ShowAllLineItems"].Value = showAllLineItems;
        ReportSource.ReportDocument = Report;
        ViewState.Remove("CalculatorID");
 
        Hashtable DeviceInfo = new Hashtable();
        RenderingResult Result = Processor.RenderReport("PDF", ReportSource, DeviceInfo);
 
        string FileName = CalculatorClientInfo.Rows[0]["ClientName"].ToString() + "-Pricing." + Result.Extension;
 
        Response.Clear();
        Response.ContentType = Result.MimeType;
        Response.Cache.SetCacheability(HttpCacheability.Private);
        Response.Expires = -1;
        Response.Buffer = true;
 
        Response.AddHeader("Content-Disposition", string.Format("{0};FileName=\"{1}\"", "attachment", FileName));
        Response.BinaryWrite(Result.DocumentBytes);
        Response.End();
 
    }

<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
            <script type="text/javascript">
                function callbackFn(arg) {
                    if (arg) {
                        $find("<%=RadAjaxManager1.ClientID %>").ajaxRequest(arg);
                    }
 
                }
            </script>
        </telerik:RadCodeBlock>
    <telerik:RadWindowManager ID="RadWindowManager1" runat="server">
         <PromptTemplate>
                <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
                    <script type="text/javascript">
                        function closePrompt(winid) {
                            var select = document.getElementById('select' + winid);
                            var selectedValue = select.options[select.selectedIndex].value;
 
                            var confirmWnd = $find(winid);
                            confirmWnd.close(selectedValue);
 
                        }
                    </script>
                </telerik:RadCodeBlock>
                <div class="windowpopup radprompt">
                    <div class="dialogtext">
                        {1}
                    </div>
                    <div>
                        <select id="select{0}">
                            <option value="Yes">Yes</option>
                            <option value="No">No</option>
                        </select>
                    </div>
                    <div>
                        <input type="button" onclick="closePrompt('{0}');" value="OK" />
                        <input type="button" onclick="$find('{0}').close();" value="Cancel" />
                    </div>
                </div>
                </div>
            </PromptTemplate>
    </telerik:RadWindowManager>

1 Answer, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 16 Jun 2015, 07:22 AM

Hi Steven,

You cannot send files from the server during partial page rendering (i.e., an AJAX request or partial postback). Sending a file requires changing the server resposne content and headers, and MS AJAX does not allow that.

Thus, you need to invoke a full postback from the callback function (e.g., by using the __doPostBack() function, perhaps a hidden button and/or a hidden field).

Here is a basic example

<asp:HiddenField runat="server" id="confirmValue" />
<asp:Button ID="Button1" Text="" style="display: none; position: absolute; left: -9999px; top: -9999px;" runat="server" OnClick="Button1_Click" />
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
    <script type="text/javascript">
        function callbackFn(arg) {
            if (arg) {
                $get("<%=confirmValue.ClientID%>").value = arg.toString();
                __doPostBack("<%=Button1.UniqueID%>", "");
            }
        }
    </script>
</telerik:RadCodeBlock>

protected void Button1_Click(object sender, EventArgs e)
{
    if (confirmValue.Value == "true")
        ExportCalculator(true);
    else if (confirmValue.Value == "false")
        ExportCalculator(false);      
}


Regards,

Marin Bratanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Steven
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Share this question
or