Unable to download file using AsyncPostBackTrigger in RadGrid

19 posts, 0 answers
  1. Dorababu
    Dorababu avatar
    95 posts
    Member since:
    May 2012

    Posted 25 Jan 2013 Link to this post

    Hi all I am using RadGrid in my application, as I want my RadGrid not to refresh I had my RadGrid in an Update Panel as follows
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
                <Triggers>
                <asp:AsyncPostBackTrigger ControlID="RadGrid1" EventName="ItemCommand" />
                    <%--<asp:PostBackTrigger ControlID="RadGrid1" />--%>
                </Triggers>
                <ContentTemplate>
                    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnItemCommand="RadGrid1_ItemCommand"
                        OnNeedDataSource="RadGrid1_NeedDataSource">
                        <MasterTableView Width="950" AutoGenerateColumns="false" DataKeyNames="EmpID" GridLines="None"
                            TableLayout="Auto">
                            <Columns>
                                <telerik:GridBoundColumn DataField="EmpID" HeaderText="Emp ID" ReadOnly="true" HeaderStyle-HorizontalAlign="Left"
                                    ItemStyle-HorizontalAlign="Left" UniqueName="EmpID" FilterControlWidth="30px"
                                    AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" />
                                <telerik:GridButtonColumn DataTextField="ButtonName" ItemStyle-ForeColor="Blue" CommandName="Generate"
                                    ConfirmTextFields="ButtonName" ConfirmTextFormatString="Would you like to  {0} ACH file ?"
                                    ConfirmDialogType="RadWindow" Reorderable="false" UniqueName="ButtonName" ConfirmTitle="ACH File">
                                </telerik:GridButtonColumn>
                                <telerik:GridBoundColumn DataField="EmployeeName" HeaderText="Employee Name" ReadOnly="true"
                                    HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" UniqueName="EmployeeName"
                                    FilterControlWidth="60px" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" />
                            </Columns>
                        </MasterTableView>
                    </telerik:RadGrid>
                </ContentTemplate>
            </asp:UpdatePanel>

    When I click on download button I am unable to download the file this is my code in Itemcommand

    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Generate")
            {
                Response.ContentType = "text/plain";
                Response.AppendHeader("Content-Disposition", "attachment;filename= errorLog.txt");
                Response.AddHeader("content-length", "0");
                Response.Flush();
                Response.End();
            }
        }
    Can some one help me how can I work out this using AsyncPostBackTrigger
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 28 Jan 2013 Link to this post

    Hello,

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = e.Item as GridDataItem;
                ScriptManager.GetCurrent(Page).RegisterPostBackControl(item["ButtonName"].Controls[0]);
     
            }
        }


    Thanks,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Dorababu
    Dorababu avatar
    95 posts
    Member since:
    May 2012

    Posted 30 Jan 2013 Link to this post

    didn't worked
  5. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 30 Jan 2013 Link to this post

    Hello,

    Please try with below code snippet.

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridWithEnum.aspx.cs" Inherits="GridWithEnum" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
            </telerik:RadScriptManager>
            <telerik:RadWindowManager ID="RadWindowManager1" runat="server">
            </telerik:RadWindowManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
                <ContentTemplate>
                    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnItemCommand="RadGrid1_ItemCommand"
                        OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound">
                        <MasterTableView Width="950" AutoGenerateColumns="false" DataKeyNames="ID" GridLines="None"
                            TableLayout="Auto">
                            <Columns>
                                <telerik:GridBoundColumn DataField="ID" HeaderText="Emp ID" ReadOnly="true" HeaderStyle-HorizontalAlign="Left"
                                    ItemStyle-HorizontalAlign="Left" UniqueName="ID" FilterControlWidth="30px" AutoPostBackOnFilter="true"
                                    CurrentFilterFunction="Contains" />
                                <telerik:GridButtonColumn DataTextField="Name" ItemStyle-ForeColor="Blue" CommandName="Generate"
                                    ConfirmTextFields="Name" ConfirmTextFormatString="Would you like to  {0} ACH file ?"
                                    ConfirmDialogType="RadWindow" Reorderable="false" UniqueName="ButtonName" ConfirmTitle="ACH File">
                                </telerik:GridButtonColumn>
                            </Columns>
                        </MasterTableView>
                    </telerik:RadGrid>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
        </form>
    </body>
    </html>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Web.UI;
    using System.Data;
     
    public partial class GridWithEnum : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
     
        }
     
        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            dynamic data = new[] {
                 new { ID = 1, Name ="Name_1"},
                   new { ID = 2, Name = "Name_2"},
                   new { ID = 3, Name = "Name_3"},
                   new { ID = 4, Name = "Name_4"},
                   new { ID = 5, Name = "Name_5"}
            };
     
            RadGrid1.DataSource = data;
     
        }
     
     
        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = e.Item as GridDataItem;
                ScriptManager.GetCurrent(Page).RegisterPostBackControl(item["ButtonName"].Controls[0]);
     
            }
        }
     
     
        protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Generate")
            {
                Response.ContentType = "text/plain";
                Response.AppendHeader("Content-Disposition", "attachment;filename= errorLog.txt");
                Response.AddHeader("content-length", "0");
                Response.Flush();
                Response.End();
            }
        }
     
    }






    Thanks,
    Jayesh Goyani
  6. Fawad
    Fawad avatar
    20 posts
    Member since:
    Nov 2014

    Posted 09 Apr 2015 Link to this post

    Hi Jayesh,

    I applied the same formula in my page but I still get an error while downloading files for the first time the file got uploaded via Edit Template. When I reload my page, I get the same error but I can then download file.

     

    The error specifically I get is as below:

    Data = Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.

     

     

    Thank you in advance,

    M. Fawad Surosh

  7. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 10 Apr 2015 in reply to Fawad Link to this post

    Hello,

    Could you please provide your code?

    Thanks,

    Jayesh Goyani

  8. Fawad
    Fawad avatar
    20 posts
    Member since:
    Nov 2014

    Posted 10 Apr 2015 Link to this post

    Hey hi Jayesh,

    Here is my code below:

    protected void attachmentHandler(object sender, GridCommandEventArgs e, string userType)
     
    {
    try {
     const string Quote = "\"";
    GridEditableItem dataItem = (GridEditableItem)e.Item;
    string fileName = dataItem[userType + "Attachment"].Text;
    string path = Server.MapPath("~/Attachments/" + fileName);
    if (e.CommandName == "Download" + userType + "Attachment")
    {
    if (e.Item is GridEditableItem)
    {
    string name = Path.GetFileName(path);
    string ext = Path.GetExtension(path);
    string type = "";
    if (ext != null)
    {
    switch (ext.ToLower())
    {
    case ".docx":
    case ".doc":
    case ".rtf":
    type = "Application/msword";
    break;
    case ".xls":
    case ".xlsx":
    case ".csv":
    type = "Application/x-msexcel";
    break;
    default:
    type = "text/plain";
    break;
    }
    }
    Response.AppendHeader("content-disposition",
    "attachment; filename=" + name.Remove(name.IndexOf("_!!_")) + ext);
    if (type != "")
    Response.ContentType = type;
    Response.WriteFile(path);
    Response.End();
    }
    }
    }
    catch (Exception ex)
    {
    String s = ex.Message;
    }
    }

  9. Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 15 Apr 2015 Link to this post

    Hi,

    If you want to invoke download for a file from an AJAX-enabled control (either placed in RadAjaxPanel or ajaxified by RadAjaxManager), you cannot use the normal response.WriteFile function, because the XmlHttpRequest cannot handle this.
    Please strictly follow the appraoch provided by Jayesh and see if it works.


    Regards,
    Maria Ilieva
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  10. Fawad
    Fawad avatar
    20 posts
    Member since:
    Nov 2014

    Posted 16 Apr 2015 in reply to Maria Ilieva Link to this post

    Maria Ilieva said:Hi,

    If you want to invoke download for a file from an AJAX-enabled control (either placed in RadAjaxPanel or ajaxified by RadAjaxManager), you cannot use the normal response.WriteFile function, because the XmlHttpRequest cannot handle this.
    Please strictly follow the appraoch provided by Jayesh and see if it works.

     


    Regards,
    Maria Ilieva
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     

    Hi Maria and Jayesh,

    The problem is: When the users open the form template and attach their files, they cannot download it after the file is uploaded unless they reload the page. This behavior is encountered if they do any type of action (Update/Insert/Cancel) in Form template. 

    I am using AjaxManager with below code behind on ItemDataBound of grid but still no luck.

    string[] uniqueNameArr = { "DownloadAtt", "DeleteAtt"};
    foreach (string uniqueName in uniqueNameArr)
    {                       ScriptManager.GetCurrent(Page).RegisterPostBackControl(dataItem[uniqueName].Controls[0]);             }

  11. Fawad
    Fawad avatar
    20 posts
    Member since:
    Nov 2014

    Posted 16 Apr 2015 in reply to Fawad Link to this post

    I also added this piece of code in my download procedure as below:

     

          Response.AddHeader("content-length", "0");
          //Response.WriteFile(path);
          Response.Flush();
          Response.End();

  12. Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 20 Apr 2015 Link to this post

    Hi Fawad,

    Can you please inspect the response when the use attempts to download file first time and see if any errors appear in the browser's console?

    Regards,
    Maria Ilieva
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  13. Fawad
    Fawad avatar
    20 posts
    Member since:
    Nov 2014

    Posted 20 Apr 2015 Link to this post

    Hi Maria,

    below is the error that I traced from Chrome browser:

    Uncaught Sys.WebForms.PageRequestManagerParserErrorException: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.

    Onething more I have to add that even by injecting page reload: when I am returning to another page by pageNavigator, I still can't download files and get the same error although I can download anything on my first page.

  14. Fawad
    Fawad avatar
    20 posts
    Member since:
    Nov 2014

    Posted 21 Apr 2015 in reply to Maria Ilieva Link to this post

    Any idea on my post please?
  15. Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 22 Apr 2015 Link to this post

    Hi Fawad,

    The method for registering the download link as PostBakc control works properly only in case the control is ajaxified with RadAjaxPanel control. Can you try using RadAjaxPanel instead of RadAjaxManager and see how it goes?

    Regards,
    Maria Ilieva
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  16. Fawad
    Fawad avatar
    20 posts
    Member since:
    Nov 2014

    Posted 22 Apr 2015 in reply to Maria Ilieva Link to this post

    Hey Maria,

    You are awesome, RadAjaxPanel worked for me!

    I still don't understand why it was not working with RadAjaxManager.

  17. Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 22 Apr 2015 Link to this post

    Hi,

    The reason is that the RegisterPostbackControl method is not applicable when the page is AJAX-enabled with RadAjaxManager.
    This is described in the help topic below:
    http://www.telerik.com/help/aspnet-ajax/ajax-exclude.html

    Regards,
    Maria Ilieva
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  18. Fawad
    Fawad avatar
    20 posts
    Member since:
    Nov 2014

    Posted 25 Apr 2015 in reply to Maria Ilieva Link to this post

    Thank you Maria. Got it.
  19. Rashmi
    Rashmi avatar
    1 posts
    Member since:
    Dec 2015

    Posted 08 Dec 2015 in reply to Maria Ilieva Link to this post

    Hi Maria and Fawad,,

     

    Can you please tell me how to use RadAjaxPanel instead of RadAjaxManager

  20. Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 11 Dec 2015 Link to this post

    Hi Rashmi,

    You can warp all the control that you want to perform Ajax in a RadAjaxPanel control, instead of adding settings in RadAjaxManager. In case you are facing any problems in using the RadAjaxPanel on the page you can share your scenario so that we can suggest the best approach in your case.

    Regards,
    Maria Ilieva
    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
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017