AJAX UpdateProgress, FileDownload & Redirect To download page

The Scenario:
The AJAX UpdateProgress control & filedownload functionality together sometimes can get tricky.
I have a GridView control inside an UpdatePanel.
This grid displays some file information & a download button on each row.
On click of the download button, that file gets downloaded
The download logic is in another separate aspx file called Download.aspx.
Now, on the download button click, if I do a “Response.Redirect” from code-behind(server side),– to go to download.aspx page , the async postback on the parent page never gets a chance to end.
The request goes to server — and the download code ends/clears that response to pop-up the download dialog box.
If you put a breakpoint anywhere in parent page’s, “PageRequestManager-endrequest event handler”, you would notice it never gets a chance to run, after you click download.
Hence the UpdateProgress kinda hangs on the parent page even after the download pop-up box had shown up & the user has chosen to either save/open/cancel.

The Solution:
The solution which worked for me was to — redirect to the download page at the end of request through javascript — in PageRequestManager-endrequest event handler.
I added the delay in the RowCommand handler and when all the server logic is executed & asyn postback is about to be all done (endRequest handler), I redirect to download.aspx
I added some extra logic to make sure that the file download pops-up only when the download button is hit & no other button in that page.

Here’s some partial relevant code snippet, to give you an idea.
(a) Some partial relevant html
<asp:ScriptManager ID=”ScriptManager1″ runat=”server” />
<script type=”text/javascript” language=”javascript”>
var DownloadBtn;
var DownloadFile;
function SaveTheDownloadBtn(MyArgs) {
var index = MyArgs.indexOf(‘,’);
DownloadBtn = MyArgs.substring(0, index);
DownloadFile = “Download.aspx?filename=”+MyArgs.substring(index+1);
function GoToDownload(strFilename) {
window.location.href = strFilename;
var prm = Sys.WebForms.PageRequestManager.getInstance();
var postBackElement;
function InitializeRequest(sender, args) {
postBackElement = args.get_postBackElement();
function EndRequest(sender, args) {
if (DownloadBtn == postBackElement.id) {
function beginRequest(sender, args) {
if (DownloadBtn == postBackElement.id) {
var tabContainer = $get(postBackElement.id);
var _updateProgressDiv = $get(”);
var tabContainerwBounds = Sys.UI.DomElement.getBounds(tabContainer);
var updateProgressDivBounds = Sys.UI.DomElement.getBounds(_updateProgressDiv);
// place the progress at the center of download button
var x = tabContainerwBounds.x + Math.round(tabContainerwBounds.width / 2) – Math.round(updateProgressDivBounds.width / 2)-37;
var y = tabContainerwBounds.y + Math.round(tabContainerwBounds.height / 2) – Math.round(updateProgressDivBounds.height / 2)-15;
Sys.UI.DomElement.setLocation(_updateProgressDiv, x, y);
<asp:UpdatePanel ID=”updatePanel” runat=”server” UpdateMode=”Conditional” >
<asp:GridView ID=”gvFiles” runat=”server” GridLines=”None”
AllowPaging=”false” AllowSorting=”true” AutoGenerateColumns=”false”
PageSize=”10″ OnRowCreated=”gvFiles_RowCreated” CellPadding=”10″
CellSpacing=”0″ Width=”900px” onrowcommand=”gvFiles_RowCommand”
onrowdatabound=”gvFiles_RowDataBound” EnableViewState=”true”
DataKeyNames=”fileID” >
<AlternatingRowStyle BackColor=”aliceBlue” />
<HeaderStyle HorizontalAlign=”Left” BackColor=”Gray” />
<RowStyle HorizontalAlign=”Center” />
<PagerStyle HorizontalAlign=”Right” />
<asp:BoundField DataField=”fileID” HeaderText=”fileID” />
<asp:BoundField DataField=”filePath” HeaderText=”filePath” />
<asp:BoundField DataField=”fileName” HeaderText=”fileName” />
<asp:Button ID=”btn1″ runat=”server” Text=”Download” CommandName=”abc1″ CommandArgument='<%# DataBinder.Eval(Container, “RowIndex”) %>’ />
<br />
<asp:UpdateProgress ID=”updProgressTab” AssociatedUpdatePanelID=”updatePanel” runat=”server” >
<div style=”background:White”>
<asp:Image ID=”imgLoading” runat=”server” ImageUrl=”simple.gif” Width=”34px” />Loading…
(b)Now in codebehind file —
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
protected void gvFiles_RowCommand(object sender, GridViewCommandEventArgs e)
if (e.CommandName == “abc1”)
int index = int.Parse(e.CommandArgument.ToString());
GridViewRow selectedRow = gvFiles.Rows[index];
Button btn1 = (Button)selectedRow.FindControl(“btn1”);
//Response.Redirect(“Download.aspx?filename=” + selectedRow.Cells[1].Text);
/*This won’t work because the the PageRequestManager endrequest function never gets a chance to execute. Use JS to redirect*/

protected void gvFiles_RowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
Button btn1 = (Button)e.Row.FindControl(“btn1”);
btn1.Attributes.Add(“onclick”, “SaveTheDownloadBtn(‘” + btn1.ClientID + “,” + e.Row.Cells[1].Text + “‘);”);

private void BindGrid()
gvFiles.DataSource = FileFields.GetMyFiles();
(c) Finally download.aspx.cs file—
protected void Page_Load(object sender, EventArgs e)
string strFileName = Request.QueryString[“filename”].ToString();
Response.AddHeader(“content-disposition”, string.Format(“attachment; filename={0}”, strFileName));
Response.ContentType = “application/octet-stream”;

That’s one solution, I found, works. Let me know. Thanks for reading.

This entry was posted in AJAXToolKit and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s