<
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();
}
}
AsyncPostBackTrigger
18 Answers, 1 is accepted
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
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">
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
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
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
Hello,
Could you please provide your code?
Thanks,
Jayesh Goyani
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;
}
}
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.
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]); }
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();
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.
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.
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.
Hey Maria,
You are awesome, RadAjaxPanel worked for me!
I still don't understand why it was not working with RadAjaxManager.
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.
Hi Maria and Fawad,,
Can you please tell me how to use RadAjaxPanel instead of RadAjaxManager
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