[Solved] Error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed

samsmuthu
 
on Sep 10, 2021 10:57 PM
Sample_195779.zip
703 Views

Hi 

Reference of article Download multiple files as Zip Archive File using GridView in ASP.Net. I have developed a follow code. i want to change image filename when downloading.

My problem was

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

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
	<asp:UpdatePanel ID="UpdatePanel2" runat="server">
		<ContentTemplate>
			<asp:Button ID="BtnShow" runat="server" Text="Show" />
			<asp:Button ID="btnDownload" runat="server" Text="Download" OnClick="DownloadFiles" /></ContentTemplate>
	</asp:UpdatePanel>
	<asp:UpdatePanel ID="UpdatePanel1" runat="server">
		<ContentTemplate>
		<asp:GridView ID="GridView1" runat="server"
			AutoGenerateColumns="false" AllowPaging="true"
			OnPageIndexChanging="GridView1_PageIndexChanging"
			PageSize="25" CssClass="GridViewStyle"
			EmptyDataText="No files available">
			<Columns>
				<asp:TemplateField HeaderText="" ItemStyle-Wrap="false" ItemStyle-VerticalAlign="Middle" ItemStyle-HorizontalAlign="Left">
					<ItemTemplate>
						<asp:CheckBox ID="ChkSelect" runat="server" />
					</ItemTemplate>
				</asp:TemplateField>
				<asp:TemplateField HeaderText="File name" ItemStyle-Wrap="true" ItemStyle-VerticalAlign="Middle" ItemStyle-HorizontalAlign="Left">
					<ItemTemplate>
						<asp:Label ID="LblNewPhotoFileName" runat="server" Text='<%# Eval("NewPhotoFileName")%>' Visible="true"></asp:Label>
						<asp:Label ID="LblPhotoFilePath" runat="server" Text='<%# Eval("PhotoFileName") %>' Visible="false"></asp:Label>
					</ItemTemplate>
				</asp:TemplateField>
				<asp:TemplateField HeaderText="Photo" ItemStyle-Wrap="false" ItemStyle-VerticalAlign="Middle" ItemStyle-HorizontalAlign="Center">
					<ItemTemplate>
						<asp:Image ID="ImgPhoto" runat="server" BorderWidth="1px" Style="max-width: 150px; max-height: 100px;" Visible="true" ImageUrl='<%# Eval("PhotoFilePath")%>' />
					</ItemTemplate>
				</asp:TemplateField>
			</Columns>
		</asp:GridView>
		</ContentTemplate>
	</asp:UpdatePanel>
</asp:Content>

VB.NET 

Public Class Project_Photo_Download
    Inherits System.Web.UI.Page
    Dim connWF As New SqlConnection(ConfigurationManager.ConnectionStrings("constrPM").ConnectionString)
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        PageAllow()
        If Not Page.IsPostBack Then
            BindData_DdlProjectName()
            BindData_DdlMonth()

            Dim filePaths As String() = Directory.GetFiles(Server.MapPath("~/WorkForce/ASST/Img/"))
            Dim files As New List(Of ListItem)()
            For Each filePath As String In filePaths
                files.Add(New ListItem(Path.GetFileName(filePath), filePath))
            Next
        End If
    End Sub
	 Private Sub BindData_GridView1()
        Using cmd As New SqlCommand("Project_Photo_ProjectID")
            cmd.Connection = connWF
            cmd.CommandType = CommandType.StoredProcedure           

            Using sda As New SqlDataAdapter()
                cmd.Connection = connWF
                sda.SelectCommand = cmd
                Using dt As New DataTable()
                    sda.Fill(dt)
                    GridView1.DataSource = dt
                    GridView1.DataBind()
                End Using
            End Using
        End Using
    End Sub
    Protected Sub GridView1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)
        GridView1.PageIndex = e.NewPageIndex
        Me.BindData_GridView1()
    End Sub
    Protected Sub BtnShow_Click(sender As Object, e As EventArgs) Handles BtnShow.Click
        BindData_GridView1()
    End Sub
    Protected Sub DownloadFiles(sender As Object, e As EventArgs)
        Using zip As New ZipFile()
            zip.AlternateEncodingUsage = ZipOption.AsNecessary
            zip.AddDirectoryByName("Files")
            For Each row As GridViewRow In GridView1.Rows
                If TryCast(row.FindControl("ChkSelect"), CheckBox).Checked Then
                    Dim filePath As String = TryCast(row.FindControl("LblPhotoFilePath"), Label).Text
                    zip.AddFile(filePath, "Files")
                End If
            Next
            Response.Clear()
            Response.BufferOutput = False
            Dim zipName As String = [String].Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"))
            Response.ContentType = "application/zip"
            Response.AddHeader("content-disposition", "attachment; filename=" + zipName)
            zip.Save(Response.OutputStream)
            Response.[End]()
        End Using
    End Sub
End Class

Please help me to fix my issue.

thanking you

samsmuthu

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 11, 2021 07:03 AM
on Sep 13, 2021 05:09 AM

Hi samsmuthu,

Add PostBackTrigger to your btnDownload.

HTML

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:UpdatePanel ID="UpdatePanel3" runat="server">
        <ContentTemplate>
            <asp:Button ID="BtnShow" runat="server" Text="Show" />
            <asp:Button ID="btnDownload" runat="server" Text="Download" OnClick="DownloadFiles" />
        </ContentTemplate>
        <Triggers>
            <asp:PostBackTrigger ControlID="btnDownload" />
        </Triggers>
    </asp:UpdatePanel>
    <asp:UpdatePanel ID="UpdatePanel4" runat="server">
        <ContentTemplate>
            <asp:GridView ID="GridView2" runat="server"
                AutoGenerateColumns="false" AllowPaging="true"
                OnPageIndexChanging="GridView1_PageIndexChanging"
                PageSize="25" CssClass="GridViewStyle"
                EmptyDataText="No files available">
                <Columns>
                    <asp:TemplateField HeaderText="" ItemStyle-Wrap="false" ItemStyle-VerticalAlign="Middle" ItemStyle-HorizontalAlign="Left">
                        <ItemTemplate>
                            <asp:CheckBox ID="ChkSelect" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="File name" ItemStyle-Wrap="true" ItemStyle-VerticalAlign="Middle" ItemStyle-HorizontalAlign="Left">
                        <ItemTemplate>
                            <asp:Label ID="LblNewPhotoFileName" runat="server" Text='<%# Eval("NewPhotoFileName")%>' Visible="true"></asp:Label>
                            <asp:Label ID="LblPhotoFilePath" runat="server" Text='<%# Eval("PhotoFileName") %>' Visible="false"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Photo" ItemStyle-Wrap="false" ItemStyle-VerticalAlign="Middle" ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <asp:Image ID="ImgPhoto" runat="server" BorderWidth="1px" Style="max-width: 150px; max-height: 100px;" Visible="true" ImageUrl='<%# Eval("PhotoFilePath")%>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

Namespaces

C#

using System.IO;
using Ionic.Zip;
using System.Collections.Generic;

VB.Net

Imports System.IO
Imports Ionic.Zip
Imports System.Collections.Generic

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string[] filePaths = Directory.GetFiles(Server.MapPath("~/Files/"));
        List<ListItem> files = new List<ListItem>();
        foreach (string filePath in filePaths)
        {
            files.Add(new ListItem(Path.GetFileName(filePath), filePath));
        }
        GridView1.DataSource = files;
        GridView1.DataBind();
    }
}
protected void DownloadFiles(object sender, EventArgs e)
{
    using (ZipFile zip = new ZipFile())
    {
        zip.AlternateEncodingUsage = ZipOption.AsNecessary;
        zip.AddDirectoryByName("Files");
        foreach (GridViewRow row in GridView1.Rows)
        {
            if ((row.FindControl("chkSelect") as CheckBox).Checked)
            {
                string filePath = (row.FindControl("lblFilePath") as Label).Text;
                zip.AddFile(filePath, "Files");
            }
        }
        Response.Clear();
        Response.BufferOutput = false;
        string zipName = string.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
        Response.ContentType = "application/zip";
        Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
        zip.Save(Response.OutputStream);
        Response.End();
    }
}

VB.Net

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim filePaths As String() = Directory.GetFiles(Server.MapPath("~/Files/"))
        Dim files As New List(Of ListItem)()
        For Each filePath As String In filePaths
            files.Add(New ListItem(Path.GetFileName(filePath), filePath))
        Next
        GridView1.DataSource = files
        GridView1.DataBind()
    End If
End Sub
Protected Sub DownloadFiles(sender As Object, e As EventArgs)
    Using zip As New ZipFile()
        zip.AlternateEncodingUsage = ZipOption.AsNecessary
        zip.AddDirectoryByName("Files")
        For Each row As GridViewRow In GridView1.Rows
            If TryCast(row.FindControl("chkSelect"), CheckBox).Checked Then
                Dim filePath As String = TryCast(row.FindControl("lblFilePath"), Label).Text
                zip.AddFile(filePath, "Files")
            End If
        Next
        Response.Clear()
        Response.BufferOutput = False
        Dim zipName As String = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"))
        Response.ContentType = "application/zip"
        Response.AddHeader("content-disposition", "attachment; filename=" + zipName)
        zip.Save(Response.OutputStream)
        Response.End()
    End Using
End Sub