views:

23

answers:

2

*Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled. Details: Error parsing near '�PNG

'.*

if i remove the <asp:updatepanel its all working fine

protected void gvFiles_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Download")
    {
        byte[] byteArray = item.AttachContent.ToArray();
        Response.Clear();
        Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + item.AttachFileName + "\"");
        Response.AppendHeader("Content-Length", byteArray.Length.ToString());
        Response.ContentType = "application/octet-stream";
        Response.BinaryWrite(byteArray);
        Response.End();
    }
}
A: 

As the error describes, Response.BinaryWrite is among the blacklisted activities during an asynchronous callback. This is due to the UpdatePanel's special handling of the content returned during its asynchronous callback.

I would recommend moving your attachment-download logic into a separate page, HTTP handler, or service, allow it to be accessed with a GET (by controlling parameters in the query string), and changing the element in gvFiles from whatever it is (Button, ListButton, ImageButton, etc) into a hyperlink to the new resource. By downloading on a GET from a separate resource, you will avoid the error.

kbrimington
A: 

This is occurring because you are trying to do a Response.BinaryWrite.

You get this error because because you are trying to return non HTML to the UpdatePanel using an asynchronous callback which is not allowed. The only thing I can suggest is to try using a PostBackTrigger and targetting a control outside of your UpdatePanel.

The best solution would be to make you download button spawn another window that calls an .ashx (generic handler) and server up the binary via the ashx. You can just attach a javascript function to handle the click which will open a new window to the ashx with whatever params are needed passed in.

This link has more information on the exact problem and ways of solving it.

There are several ways to fix this problem each fix has specific scops.

1.Register ButtonDownload PostBackTrigger control in Triggers child tag of the update panel as shown below.

2.You can also use RegisterPostBackControl method of the ScriptManager control in Page_Load as shown below.

Kelsey