views:

1463

answers:

8

I have a form that kicks off a Response.Redirect to download a file once complete. I also want to hide the form and show a 'thank you' panel before the redirect takes place, however it seems the asp.net engine just does the redirect without doing the 2 tasks before in the following code:

if (success)
                {
                    lblSuccessMessage.Text = _successMessage;
                    showMessage(true);                        
                }
                else
                {
                    lblSuccessMessage.Text = _failureMessage;
                    showMessage(false);
                }

                if(success)
                    Response.Redirect(_downloadURL);

Any idea how i can force the page to update before the Redirect kicks in??

Thanks heaps Greg

+2  A: 

Maybe sending a Redirect Header manually, with a number of seconds to wait is the way to go for you?

Response.AddHeader("Redirect", "3; URL=" + _downloadURL")

EDIT

After reading your question again, I may have misunderstood you a bit.

You seem to want to delay the whole process so that a "Thank You" panel can be displayed. That leaves you two possibilities:

  • Either you rely on JavaScript to display the panel and delay the form POST. The server can then redirect immediately with Response.Redirect(). This is the more modern way to do it.
  • Or you want to be fully independent from JavaScript. In this case you must make the server display an intermediate "Thank You, click here to go on" page upon the form POST, and use the Redirect header method via Response.AddHeader() to make that page go away automatically. This is a bit old-fashioned but reliable.

You cannot really have a combination of both, because that would be inconsistent for those users who have JavaScript switched off.

Tomalak
+1  A: 

You cant, because this action happens on the server before its sent back to the client. If you are trying to send a file to a user, you can stream it to them using Response.Write(). This will keep them on the current page so that you can show them the message and they will get the download prompt.

buffer is a byte array of a file

Response.AddHeader("Content-disposition", "attachment; filename=" & myUserFriendlyFileName)
Response.ContentType = "application/octet-stream"
Response.OutputStream.Write(buffer, 0, buffer.Length)
StingyJack
A: 

Thanks Andrew, but that's not quite what I'm after... the file is actually on another server so I do actually need to redirect them.. just need to change the page before doing the redirect.

what you've said about happening on the server makes sense.. seems i need to post to an interim page, right? Any other way around this (than creating another page just for the purpose of showing a message)?

Gregorius
+2  A: 

You need some client side code to do the redirect.

My preference would be to embed some javascript to do the redirect.

So, hide the form, display the message, and (at the crudest level) use a literal control to add some text like this to the page.

<script>
    location.href = "http://otherServerName/fileToDownload";
</script>

You may find that this redirect occurs before your page has had a change to display - in which case, try this in the body tag of your HTML (note the different types of quotes):

<body onload='location.href="http://otherServerName/fileToDownload";'&gt;

Remember that every post back is actually serving up a new page to the client, not just changing some properties on the current page (even if ASP.NET tries hard to pretend that it's just like a windows form)

Personally I prefer to have a separate page for each stage of the process, rather than trying to do everything in one page by showing/hiding the various bits - but I could be hopelessly out of date.

EDIT: if they have disabled javascript, you could just provide a link to download the file.

Robin Bennett
thanks Robin... using client-side javascript would achieve what I'm after, but I still need to write code to make it accessible to users without javascript enabled... so i'll still need to write that extra page, so really it won't accomplish much unfortunately. Seems the extra page is the go.Thanks
Gregorius
+1  A: 

Directly in asp.net you cant do this, but a way around is to either use JS (as is posted on here), or you can use an IFrame that loads the file to be downloaded - the user will see the thankyou, then the Open/save dialogue...

pzycoman
+1  A: 

Hide it client-side with javascript and then do the redirect, or do two postbacks: first postback to hide the form and show the thank-you, and the second to do the redirect once the thank-you has been rendered to the screen.

Or, you could do a javascript window.open instead of a redirect that points to the destination after the postback and form hiding is completed.

Chris Ballance
A: 
if (success)
                {
                    lblSuccessMessage.Text = _successMessage;
                    showMessage(true);                        
                }
                else
                {
                    lblSuccessMessage.Text = _failureMessage;
                    showMessage(false);
                }

                if(success) {
                    Threading.Thread.Sleep(200)
                    Response.Redirect(_downloadURL);
                }

You can force it to wait before it redirects by making the thread sleep.

The best, and most user friendly option is to let the user continue on their own by adding a button. For instance, you could do the following:

if (success)
                {
                    lblSuccessMessage.Text = _successMessage + "<br /><INPUT TYPE='button' VALUE='Continue...' onClick='parent.location='" + _downloadURL + "'/>";
                    showMessage(true);                        
                }
                else
                {
                    lblSuccessMessage.Text = _failureMessage;
                    showMessage(false);
                }
no sorry, that's wrong. Thread.Sleep on the server just makes the server wait 200ms before redirecting. Also, it's bad form in asp.net to mix code and markup (i.e. don't create controls in strings)
Robert Paulson
no point delaying the response on server side...
Samuel Kim
How comes an answer with -1 is the accepted one? Strange.
Tomalak
A: 

I ended up doing the suggestion by abrudtkuh, just adding a link to the succeess message... i will also implement a javascript redirect to automatically start the download, but i didn't want to rely solely on javascript, hence the button being displayed by the page.

Thanks for the tips everybody - you all rock!

cheers greg

Gregorius