I'm using a DataGridView to display a list of statements. One of the columns is a LinkButton, which allows you to download that specific statement in PDF format. My code works perfectly in ALL BROWSERS, except for IE7 & IE8. I have no idea why that is.
<asp:GridView ID="dgvEStatements" runat="server" EnableSortingAndPagingCallbacks="False"
EnableViewState="true" GridLines="Vertical" Width="100%" AutoGenerateColumns="False"
CssClass="gridheader" EmptyDataText="<%$ Resources:IBEStatements, dgvEStatements_NoRows %>"
OnPageIndexChanging="dgvEStatements_PageIndexChanging" OnRowCommand="dgvEStatements_RowCommand"
OnRowDataBound="dgvEStatements_RowDataBound">
<Columns>
<asp:BoundField DataField="Date" HeaderText="<%$ Resources:IBEStatements, dgvEStatements_DateHeader %>"
HeaderStyle-CssClass="lhs">
<ItemStyle CssClass="lhs" />
</asp:BoundField>
<asp:BoundField DataField="Description" HeaderText="<%$ Resources:IBEStatements, dgvEStatements_DescriptionHeader %>"
HeaderStyle-CssClass="lhs" />
<asp:BoundField DataField="DocumentType" Visible="false" HeaderText="<%$ Resources:IBEStatements, dgvEStatements_DocumentTypeHeader %>"
HeaderStyle-CssClass="lhs">
<ItemStyle CssClass="lhs" />
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDownloadEStatement" runat="server" Text="<%$ Resources:IBEStatements, lnkDownloadEStatement %>" />
</ItemTemplate>
<ItemStyle CssClass="rhs" />
</asp:TemplateField>
</Columns>
</asp:GridView>
The RowDataBound event for the Grid does the following:
protected void dgvEStatements_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnkEStatement = (LinkButton)e.Row.FindControl("lnkDownloadEStatement");
string fileId = DataBinder.Eval(e.Row.DataItem, "StatementID").ToString();
lnkEStatement.Attributes.Add("onclick", "javascript:EStatementDownload('" + fileId + "'); return false;");
}
}
Javascript function to call the page that creates the PDF:
function EStatementDownload(fileid) {
var iframe = document.createElement("iframe");
iframe.src = "EStatementFile.ashx?fileid=" + fileid;
iframe.style.display = "none";
document.body.appendChild(iframe);
}
Finally, the code behind for EStatementFile.ashx looks like this:
public void ProcessRequest(HttpContext context)
{
try
{
string args = context.Request.QueryString["fileid"].ToString();
int statementID = 0;
int.TryParse(args, out statementID);
string documentID = String.Empty;
string accountnumber = String.Empty;
DateTime fileDate = DateTime.MinValue;
foreach (EStatement item in EStatementListing.EStatements)
{
if (statementID == item.StatementID)
{
documentID = item.DocumentID;
accountnumber = item.AccountNumber;
fileDate = item.DocumentDate;
break;
}
}
EStatementFacade estatementFacade = new EStatementFacade();
EStatement estatement = estatementFacade.GetEStatement(documentID, accountnumber, fileDate);
if (estatement.Document != null)
{
context.Response.Clear();
context.Response.ContentType = "Application/pdf";
context.Response.Cache.SetCacheability(HttpCacheability.Private);
context.Response.AppendHeader("Cache-Control", "private; must-revalidate");
context.Response.AppendHeader("Pragma", "private");
context.Response.AddHeader("content-disposition", "attachment; filename=" + fileDate.ToString("ddMMyyyy") + ".pdf");
context.Response.BinaryWrite(estatement.Document);
context.Response.Flush();
}
}
catch (Exception ex)
{
}
finally
{
context.ApplicationInstance.CompleteRequest();
}
}
When the Linkbutton on the grid is clicked, the following javascript information is displayed in Firebug, which might be useful in finding the issue:
Something interesting to note, if I call context.Response.End() directly after context.Response.Flush(), I get the following exception. Now the file download dialog is still being displayed in all browsers regardless of the exception, but in IE7 & IE8, still no download dialog.
context.Response.End(); 'context.Response.End()' threw an exception of type 'System.Threading.ThreadAbortException' base {System.SystemException}: {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.} ExceptionState: Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.
It might have something to do with the iFrame?
PS: Save the last image to see large image