views:

23

answers:

1

I am generating an Excel file upon a click of a button in an update panel. It is throwing a parsing error. Please see below code.

If I keep the button outside the update panel it is working fine. Why isn't it working in the update Panel?

Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", 
                    String.Format("attachment;filename={0}", filename))
Response.Clear()
Response.BinaryWrite(WriteToStream.GetBuffer)
HttpContext.Current.ApplicationInstance.CompleteRequest()

Private Function WriteToStream() As MemoryStream
    'Write the stream data of workbook to the root directory
    Dim file As MemoryStream = New MemoryStream
    hssfworkbook.Write(file)
    Return file
End Function
A: 

You shouldn't be overwriting the response for an update panel. The way the ASP update panels work, the entire page executes, and the portion within the update panel is pulled from the response and sent to the client (as a response to an XHRequest). When you do a Response.BinaryWrite, you are obliterating the standard output that the server is expecting to be able to parse through for the new markup it wants to send.

The only reason to have the button trigger the update panel is if you want to change the content within that panel. Otherwise, simply make sure the button is not a trigger of the panel. If the button must stay in the panel, add it to the panel's trigger section as a PostBackTrigger (note the lack of Async on that).

E.g.:

<asp:UpdatePanel ID="somePanel" UpdateMode="Conditional" runat="server">
  </ContentTemplate>
    changing content?
    <asp:Button id="someButton" Text="click me!" 
                OnClick="someButton_Click" runat="server">
    other changing content?
  </ContentTemplate>
  <Triggers>
    <asp:PostBackTrigger ControlID="someButton" />
  </Triggers>
</asp:UpdatePanel>
jball
Thank you so much.. it works for me.
James123