Click here to Skip to main content
15,843,498 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a link button in Gridview and clicking on it should open the file that is associated with it. The error I am getting is
JavaScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.


VB
Protected Sub btnDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs)
      Try
          '  Dim grdrow as GridViewRow =DirectCast((LinkButton),sender.NamingContainer, GridViewRow)
          Dim filePath As String = CType(sender, LinkButton).CommandArgument
          If Not filePath = "" Then
              Response.ContentType = ContentType
              Response.AppendHeader("Content-Disposition", ("attachment; filename=" + Path.GetFileName(filePath)))
              Response.WriteFile(filePath)
              Response.OutputStream.Close()
              ' HttpContext.Current.Response.End()
          Else
              ScriptManager.RegisterStartupScript(Me, Me.GetType(), "script", "alert('No file attached');", True)
          End If
      Catch ex As Exception
          ScriptManager.RegisterStartupScript(Me, Me.GetType(), "script", "Error Downloading the File", True)
          LogError.LogErrorIntoTextFile(ex, "btnDownload_Click()")
      End Try
  End Sub


can anyone help me in solving this error?
Posted
Comments
ZurdoDev 24-Feb-15 16:07pm    
What is the error on the server side?
sudevsu 24-Feb-15 16:42pm    
I am not sure. It only shows
JavaScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.
ZurdoDev 24-Feb-15 16:44pm    
Put a breakpoint in the VB code and see what the exception is.
sudevsu 24-Feb-15 16:47pm    
Alright I did that until the control finish executing btnDownload_Click function, there is no error. Only after that it take me to Script resource file and throws javascript error
sudevsu 24-Feb-15 16:43pm    
There is nothing to parse. I guess its because of Respose.WriteFile and OutputStream.

1 solution

Try adding the scriptmanager (say---> sm1)to the page or masterpage(markup),
now declare a variable(sm) in the webpage codebehind where the grid view is present( ScriptManager sm;)

Now in the row databound event of the gridview do the following

C#
if (e.Row.RowType == DataControlRowType.DataRow)
{
sm = ScriptManager.GetCurrent(this.Page);
sm.RegisterPostBackControl(e.Row.FindControl("btnDownload"));
}


Note: I did it in C# you can try this vb and let me know if this works
 
Share this answer
 
v4
Comments
sudevsu 24-Feb-15 16:36pm    
Thank you. I can do in VB. But why you need this? I already have Script manager in ASPX bcoz my grid is in Update panel. I am not sure why we need to this. Instead we can add post back Trigger in ASPX page right! Correct me if I am wrong.
santosh_131 25-Feb-15 10:17am    
btnDownload is inside the gridview so you have to register it in the row databound event

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900