views:

543

answers:

2

I am trying to place an action to happen after an entire .aspx page displays. The "action" is a function that uses the Response object to send a file to the user.

More detailed information:
I am trying to replicate the behavior of a link on the page, from a sidebar. I.E. I have a link on the main page for the Export action, and it works fine -- since the page is already displayed before the user clicks it. But when the user is on a sidebar, clicks the link, it should take them back to this main page and then send the file after it displays.

I did some research and thought that using the PageComplete event would be well-suited for this, so I created my event handler and put the call to the export code (it keys off of a query string when loaded from the sidebar) inside my PageComplete event handler. But it behaves just the same way - the browser download box pops up and the page is never loaded before or after.

If it helps to understand what I'm doing here is a snippet of the code used to send the list to the user.

Response.Clear();
Response.BufferOutput = true;
Response.ContentType = "application/ms-excel";
Response.AppendHeader("content-disposition", "attachment;filename=MyList.xls");
Response.Write(listManager.ExportLists(mycode));
Response.End();

I would prefer a way to use a page event to load the page, rather than tinkering with this logic. But, if there is a clean and easier way to get the file sent, and it allows for loading the page then sending the file, that would be fine too.

Is there another Page event I can use besides PageComplete, or is it possible I am missing something?

EDIT: Sorry about the verbosity. I realize that I can't change the way HTTP requests work - I'm only looking for an acceptable solution that achieves more or less the same results. It seems like the way to go is to force a refresh of the page after a couple of seconds (thus ensuring that it loads before the file download code is executed) -- so I am looking for a way to do this as the first answer suggests - refresh to a download. (It doesn't have to be delayed either, if there's a way to refresh with no waiting)

+2  A: 

The server can only return one object to the user, a file download or a page. The best you can manage is to return the user to a page that refreshes to a file download.

ck
The way of the web: 1 request, 1 response, and always in that order.
Joel Coehoorn
... and 1 (RESTful) URI ! ;-)
Cerebrus
How do I refresh to a file download?
n2009
A: 

Why doesn't this code work?

        private void Page_LoadComplete(object sender, System.EventArgs e)
    {
        if (Request.QueryString["action"] != null)
        {
            if (Request.QueryString["action"] == "export")
            {
                Response.Redirect("MyHome.aspx?action=exportnow", false); 
            }

            if (Request.QueryString["action"] == "exportnow")
            {
                ExportMasterList();
            }
        }
    }

What it's supposed to do: after page loading is complete, do a Response.Redirect, reloading itself with a different query string. When it reaches the Page LoadComplete event again, the second time it will trigger the function which writes out the file.

What it actually does: Apparently repeats the same problem twice... it goes back to the same problem, how do you execute an action after the page loads, or wait until the page completely finishes loading, then trigger a refresh which will execute the action? Is there no way for ASP.NET to do something by itself without the user clicking on something?

If that's the case, then an auto-refresh after 2 seconds would also be acceptable... but I'm not sure how to do that.

n2009
Instead of "answering" your own question, you should edit your question to include new information. Only answer your own question if you have found a solution better than the presented ones.
Cerebrus