Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.

Eduardo Gomez 3,671 Reputation points
2022-10-26T09:26:52.243+00:00

I have a button

<asp:LinkButton runat="server" ID="botonPfd" OnClick="BotonPdf_Click">
<i class="fa fa-file-pdf-o" style='color:#007bff;font-size:20px;cursor:pointer;' title="Generar pdf"></i>
</asp:LinkButton>

I click on a button, and run a method that generates a pdf'

public string gererar(Trabajo trabajo)
{
string plantilla = obtenerPlanilla();
plantilla = rellenarDetallesTrabajo(plantilla, trabajo);

        StringBuilder tablas = new StringBuilder();  
        List<Anotacion> anotaciones = trabajo.getAnotaciones();  
        for (int i = 1; i <= 7; ++i)  
        {  
            List<Anotacion> anotacionesTipo = (from a in anotaciones  
                                               where a.IdTipoAnotacion == i  
                                               select a).ToList();  
            if (anotacionesTipo.Count > 0)  
            {  
                string tabla = obtenerTabla().Replace("#TipoAnotacion#", ((ETiposAnotacion)i).ToString());  
                StringBuilder filas = new StringBuilder();  

                foreach (var anotacion in anotacionesTipo)  
                {  
                    filas.Append(obtenerFila().Replace("#Fecha#", anotacion.FechaAltaString)  
                                              .Replace("#NombreRecurso#", anotacion.Recurso.Nombre)  
                                              .Replace("#Contenido#", anotacion.Contenido));  
                }  
                tabla = tabla.Replace("#Filas#", filas.ToString());  
                tablas.Append(tabla);  
            }  
        }  

        plantilla = plantilla.Replace("#Tablas#", tablas.ToString());  

        var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();  
        var pdfBytes = htmlToPdf.GeneratePdf(plantilla);  

        var filename = "C:/Users/RFPORTAG-local/Desktop/ed.pdf";  

        File.WriteAllBytes(filename, pdfBytes);  

        if (!string.IsNullOrEmpty(filename))  
        {  
            return filename;  
        }  

        return null;  
    }  

I can see that the pdf was generated in my desktop, but I am triyng to download it

Dim GenerarPdf = New GenerarPdfAnotaciones

        Dim url = GenerarPdf.gererar(id)  

        Dim client = New WebClient()  
        Dim buffer = client.DownloadData(url)  
        Response.ContentType = "application/pdf"  
        Response.AddHeader("content-length", buffer.Length.ToString())  
        Response.BinaryWrite(buffer)  
Developer technologies | VB
Developer technologies | ASP.NET | Other
Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 30,191 Reputation points Microsoft External Staff
    2022-10-27T02:35:20.193+00:00

    Hi @Eduardo Gomez ,
    While asking a question you need to provide a minimal reproducible example.

    Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.

    This error can be caused if you put the button in the Updatepanel.
    You can keep the button outside the update panel. If you don't want to move it, just add a trigger for the button on the update panel, the postback trigger.

    <Triggers>  
    <asp:PostBackTrigger ControlID="botonPfd" />  
    </Triggers  
    

    Or add code under the backend LinkButton:

    Dim scriptManager As ScriptManager = scriptManager.GetCurrent(Me.Page)  
    scriptManager.RegisterPostBackControl(Me.YourButtonNameHere)  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.