Problems with ScriptManager.RegisterClientScriptBlock

0

My function btnDownloadArchives_Click well summarized.

protected void btnDownloadArquivos_Click(object sender, EventArgs e)
{
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment; filename= " + nomeArquivo);
            Response.CacheControl = "Private";

           //Aqui dentro tem uma outra função qualquer.


            Response.Flush();
            Response.Clear();
            Response.ClearContent();
            HttpContext.Current.ApplicationInstance.CompleteRequest();

            EscreveErroNaTela();

}           

Inside the WriteErnaNaTel () function has the following code.

ScriptManager.RegisterClientScriptBlock(this, GetType(), "Alerta", "alert('Alguns arquivos não puderam ser baixados. Clique no botão erros para maiores informações');", true);

This code ScriptManager.RegisterClientScriptBlock works fine, the problem is that in the function btnDownload_ClickArchives has some conditions, and when entering this condition it fills some Response properties, after a Flush, Clear, Clear content.

When it fills these properties my ScriptManager does not work, no alert is displayed, what may be happening?

    
asked by anonymous 16.02.2017 / 15:54

1 answer

0

Hello, your problem is not in the instructions:

        Response.Flush();
        Response.Clear();
        Response.ClearContent();
        HttpContext.Current.ApplicationInstance.CompleteRequest();

        EscreveErroNaTela();

What is actually occurring is when you add the Header "Content-Disposition"="attachment ..." , and change the "Content-Type"="application / octet-stream ".

This causes your browser to handle the response in a different way, ie it will reload your page through the cache, and your response will treat it as a download, ie it will not interpret as an html response .

If we perform a trace in the browser itself we can see this more clearly:

NotethatintheResponseHeaderwehavetheContent-Dispositionasanattachment.

Alreadyinthisimage,asyoucanseeinResponsedetailthereisnocontentwhatsoever.

Nownoticewhathappenswhenweremove>responsewegetadifferentresponse:

Noticeintheimagethatwenolongerhavetheheaderorthecontent-typeinformed

Note also that our response is now filled with the html content processing result on the server.

In order for you to get the desired result, you will have an additional job dealing with two requests.

    
02.03.2017 / 15:47