Wednesday, February 18, 2009

ASP.NET: UpdatePanel and Response.Write()

Problem

When you are modifying Response with Response.Write() from control which is inside UpdatePanel you may receive an error "Message received from the server could not be parsed"

Solution

Solution is to add a trigger to UpdatePanel like this:

</ContentTemplate>
  <Triggers>
    <asp:PostBackTrigger ControlID="btnExportToExcel" />
  </Triggers>
</asp:UpdatePanel>

This causes an UpdatePanel to refresh the page only in control specified in Triggers section

17 comments:

  1. One question,though... My button is inside a grid view (ItemTemplate). ASP.net says it can't find it. I verified the ID, etc. Any ideas?

    ReplyDelete
    Replies
    1. you cant directly access the controls in grid views, for that you have to loop through the grid view and find control like
      for(int i=0;i<myGridView.Rows.Count;i++)
      {
      Button btnAdd=(Button)myGridView.Rows[i].FindControl("btnID");
      }
      or you can access the button by going to the click event of the button which is inside the grid view like this
      btn_Click()
      {
      Button btnAdd=(Button)sender;

      }
      Regards:
      Mudassir

      Delete
  2. You can use the UniqueID field of the button.

    ReplyDelete
  3. Thanks man. saved me hours of headaches.

    ReplyDelete
  4. Thank you very much!!!!!

    ReplyDelete
  5. Hi to all.I have the same problem.I tried all,and still not result.Here is my code.
    Dim buf As Byte() = get_byte()
    Page.Response.Clear()
    Response.ContentType = "application/pdf"
    Response.AddHeader("Content-disposition", "attachment; filename=Statment.pdf")
    Response.BinaryWrite(buf)
    Response.End()

    My get_byte()function works fine. Can anybody help me?
    I recieve following message
    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 '%PDF-1.4
    %??2 0 obj '.
    My btn_ExportPdf is in updatepanel and I also add triggers tag like this




    Please,help...thanks in advanced :)

    ReplyDelete
  6. Nice! Also if you can give an explaination as why is this working..

    Thanks!

    ReplyDelete
  7. The best solution so far.

    Thanks.

    ReplyDelete
  8. thank you bro..its working for me

    ReplyDelete
  9. Great solution. Love the simplicity.
    This code works for me where I was attempting to return a PDF.







    .. and the ASCX page contained the gridview.

    ReplyDelete
    Replies
    1. FYI: Used the Triggers / PostBackTrigger code with

      asp:PostBackTrigger ControlID="IDofAscxPageReference"

      and the ASCX page contained the gridview.

      Delete
  10. if (!Page.IsPostBack)
    {
    Response.Output.Write("Bla Bla Bla");
    }

    ReplyDelete