Using a DataGridView to display a list of statements, with one column as a LinkButton for downloading in PDF format. Works perfectly in most browsers except IE7 & IE8 - not sure why.
<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:
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 for calling the page that generates the PDF:
function EStatementDownload(fileid) {
var iframe = document.createElement("iframe");
iframe.src = "EStatementFile.ashx?fileid=" + fileid;
iframe.style.display = "none";
document.body.appendChild(iframe);
}
Code behind for EStatementFile.ashx:
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 Linkbutton is clicked, Firebug shows relevant javascript info:
Note: If I call context.Response.End() after Flush(), exception occurs but download dialog still displays in all browsers except IE7 & IE8.
context.Response.End(); 'context.Response.End()' threw 'System.Threading.ThreadAbortException' base {System.SystemException}: {...}
Possible issue with iFrame?
PS: Save image at the end for full view