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

Unable to download file using AsyncPostBackTrigger in RadGrid

18 Answers 460 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dorababu
Top achievements
Rank 1
Dorababu asked on 25 Jan 2013, 04:23 PM
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

18 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 28 Jan 2013, 11:02 AM
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
0
Dorababu
Top achievements
Rank 1
answered on 30 Jan 2013, 07:53 AM
didn't worked
0
Jayesh Goyani
Top achievements
Rank 2
answered on 30 Jan 2013, 11:11 AM
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
0
Fawad
Top achievements
Rank 1
answered on 09 Apr 2015, 07:49 AM

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

0
Jayesh Goyani
Top achievements
Rank 2
answered on 10 Apr 2015, 04:33 PM

Hello,

Could you please provide your code?

Thanks,

Jayesh Goyani

0
Fawad
Top achievements
Rank 1
answered on 11 Apr 2015, 04:38 AM

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;
}
}

0
Maria Ilieva
Telerik team
answered on 15 Apr 2015, 11:41 AM
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.

 
0
Fawad
Top achievements
Rank 1
answered on 16 Apr 2015, 05:05 AM
[quote]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.

 

[/quote]

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]);             }

0
Fawad
Top achievements
Rank 1
answered on 16 Apr 2015, 05:12 AM

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();

0
Maria Ilieva
Telerik team
answered on 20 Apr 2015, 12:02 PM
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.

 
0
Fawad
Top achievements
Rank 1
answered on 20 Apr 2015, 01:06 PM

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.

0
Fawad
Top achievements
Rank 1
answered on 21 Apr 2015, 10:42 AM
Any idea on my post please?
0
Maria Ilieva
Telerik team
answered on 22 Apr 2015, 08:39 AM
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.

 
0
Fawad
Top achievements
Rank 1
answered on 22 Apr 2015, 10:04 AM

Hey Maria,

You are awesome, RadAjaxPanel worked for me!

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

0
Maria Ilieva
Telerik team
answered on 22 Apr 2015, 02:45 PM
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.

 
0
Fawad
Top achievements
Rank 1
answered on 25 Apr 2015, 11:47 AM
Thank you Maria. Got it.
0
Rashmi
Top achievements
Rank 1
answered on 08 Dec 2015, 11:13 AM

Hi Maria and Fawad,,

 

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

0
Maria Ilieva
Telerik team
answered on 11 Dec 2015, 11:12 AM
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
Tags
Grid
Asked by
Dorababu
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Dorababu
Top achievements
Rank 1
Fawad
Top achievements
Rank 1
Maria Ilieva
Telerik team
Rashmi
Top achievements
Rank 1
Share this question
or