Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Show/Hide Panels before executing Response.Redirect

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

like image 735
Jeeby Avatar asked Oct 16 '08 11:10

Jeeby


2 Answers

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";'>

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.

like image 57
Robin Bennett Avatar answered Sep 20 '22 08:09

Robin Bennett


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)
like image 45
StingyJack Avatar answered Sep 22 '22 08:09

StingyJack