Link to home
Start Free TrialLog in
Avatar of nemali
nemali

asked on

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
Avatar of DropZone
DropZone
Flag of United States of America image

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.
Avatar of nemali
nemali

ASKER

If i dont use response.flush() then my solution doesnt work
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.
Avatar of nemali

ASKER

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
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.
I forgot to specify that the visibility of the block should be specified with css attributes.

    -dZ.
Avatar of nemali

ASKER


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
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?
Avatar of nemali

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of DropZone
DropZone
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nemali

ASKER

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
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.
Avatar of nemali

ASKER

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
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.
Forced accept.

Computer101
EE Admin