• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4897
  • Last Modified:

Server cannot set content type after HTTP headers have been sent.

Server cannot set content type after HTTP headers have been sent.

How can i solve this error i am using javascript and response.flush to show image and message to the user
but whenever response.redirect comes across i get the above error
0
nemali
Asked:
nemali
  • 8
  • 6
1 Solution
 
DropZoneCommented:
Before calling Response.Redirect(), call Response.Clear() -- and don't call Response.Flush(), or you'll force the server to start sending the HTTP response.  The Redirect is performed by sending a specific HTTP Header, but if the headers have already been sent, you can no longer do it.

     -dZ.
0
 
nemaliAuthor Commented:
If i dont use response.flush() then my solution doesnt work
0
 
DropZoneCommented:
Well, you can't have your cake and eat it too.  You either render HTTP content output, or the HTTP header redirect; you can't do them both.  That's why you get that error.

If what you want is to display a message to the user prior to redirecting, you need to do this *before* you submit the request.  Usually this is done on the client-side (perhaps on the OnClick event of a button) with javascript.  That way, when the request arrives at the server, a redirect can be sent without any hindrance.

     -dZ.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
nemaliAuthor Commented:
Hi dZ

Thanks for your reply

Specifically this is my problem i am trying to show a progress image and some message while page load is in processing and once the results are rendered to the browser i would like to hide the progress image and message(I would like to use the same solution in more than one results  page as page loading process takes  more time  but i definitely have response.redirects in other places) i used javascript and response.flush to achieve this  without recognizing that i am using response.redirect some where in my code.If i take response.flush the solution doesnt work.

Is there any approach i can follow to achieve above scenario

Thanks very much
0
 
DropZoneCommented:
Like I said, this is usually done on the client-side before posting back to the server.  The easiest thing to do is to include a <div> block in your page with visibility set to false so that it is not displayed.  This block contains the progress image, but remains hidden under normal operation.  You add javascript to the onClick event of the submit button to enable the visibility of the <div> block right before submitting.  Then the user can stare at the image and message while the page posts back.  When the new page comes back, the visibility of the block is by default set to false again.

This is how its usually done.  The progress image and message are always there, they are just not displayed until you click the button.  Does this make sense?

     -dZ.
0
 
DropZoneCommented:
I forgot to specify that the visibility of the block should be specified with css attributes.

    -dZ.
0
 
nemaliAuthor Commented:

Let me explain my scenario,I have a search page when the search button is clicked the processing takes in a .cs file and result is returned in the form of url back to search page by using this url search page redirects to the search results page.To show search is in process i used update progress bar in search page , but in  search results  page it takes long time to render the results and there is lot of processing in page load event which is not caused by the post back(which is !IsPostBack) as you said how can i add the javascript to the onclick event  in search results page because its a normal page load processing which takes time to process for this i want to show a progress image and hide it once the results are rendered
Altogether i want to show user search transition --how can i achieve this

Let me know if its not clear

Thank you for your time
0
 
DropZoneCommented:
Ah, I understand.  The long processing takes place on the target page, and the user has to wait for it to render, and you want to display a progress image and message while its processing.  Right?
0
 
nemaliAuthor Commented:
Yes and the same thing happens in more than one page of this sought so i am looking for same solution or approach which is consistent for all pages

Thanks
0
 
DropZoneCommented:
Gotcha.  Instead of issuing a redirect from the server, do it from the client when processing is complete.  This will require that you render the progress message before the long processing takes place, and at the end you inject some javascript that will perform the redirect.

<script language="javascript" type="text/javascript">
<!-- //
window.location.href = 'http://finalpage.aspx';
// -->
</script>

You can even set this at the beginning with Page.RegisterStartupScript() and it will be rendered at the VERY END of the page (which should happen when your processing is complete).

void function Page_OnLoad(object sender, eventargs e) {
    // render here the progress message
    Response.Flush(); // send it to the browser

    // strScript contains the javascript I mentioned above
    this.RegisterStartupScript('ProgressRedir', strScript);

    // -----
    // do here the loooooooong processing
    // -----
}

This may not be the most elegant solution, but it should work nicely.

One more thing I want to tell you:  You have to consider the possibility of a timeout while the page is processing and the browser is not receiving anythiing.  For this, you could "ping" the browser once in a while, by sending a single character (space?) every once in a while and flushing it out the HTTP stream.  This ensures that the browser will receive something from the server and not timeout.  You could do this every 30 seconds or so, depending on the time it takes to process.

       -dZ.
0
 
nemaliAuthor Commented:
Hi dZ,

Is there any other solution or approach i can follow,as the above solution does it work in all the cases or not  once the site is live any problems it creates just want to make sure before i approach it
Thanks
0
 
DropZoneCommented:
There shouldn't be any problems with that solution.  And you can easily extend the Page control to perform these operations so that you can re-use it.

When I said that it is not the most elegant solution, I meant that it was quick-and-dirty, but not bad.

  -dZ.
0
 
nemaliAuthor Commented:
Hi dZ,

I am not clear about this point and how to do it as well-can you please let me know how to work with below one.I really appreciate the time you spent on this

You have to consider the possibility of a timeout while the page is processing and the browser is not receiving anythiing.  For this, you could "ping" the browser once in a while, by sending a single character (space?) every once in a while and flushing it out the HTTP stream.  This ensures that the browser will receive something from the server and not timeout.  You could do this every 30 seconds or so, depending on the time it takes to process.

Thanks
0
 
DropZoneCommented:
The code I offered above should work.  As for the comment about timing out, all you need to do is output *something* to the browser while you are processing.  If your doing something in a loop, for example, at the end of each loop iteration, add something like:

    response.Write(' ');
    response.Flush();

Or better yet, if you are expecting there to be hundreds or thousands of iterations, you may want to output a character every few times, not every time, like this:

    // output every 100th time
    if (i % 100 = 0) {
       response.Write(' ');
       response.Flush();
    }
    i++;

If your code is not in a long processing loop, but just contains many things it has to do in sequence, you may want to output the character after every "section" is done.

The idea is to let the browser know that you are still there, and not timeout the HTTP connection.  But again, this is only if your processing takes a very long time (longer than a minute or so).  For a few seconds, this is not needed.

    -dZ.
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now