Export Gridview to excel inside an update panel

Posted: Monday, February 18, 2008 in asp.net, Microsoft, MVP

When you want to export a gridview to excel it can be bit tricky task, and I bloged about some of the errors will face some time back..

Any way if you using a update panel and trying to export the gridview data into excel, you will face an issue. Even if you put EnableEventValidation=”false”

The issue is, When you are exporting gridview data into excel you normally use the response.Write to write the gridview’s RenderControl HTML . But when you use the response.Write inside an update panel it will give this error.  


update panel error

This happens because you are trying to modify HTML data in an asynchronies postback.

So there are two solutions for this
1)      you can add a PostBackTrigger and give it’s ControlID as the excel export button’s ID EX:-
      <asp:PostBackTrigger ControlID=”btnExcel” />
2)      or else you can move your excel export button out side the update panel

  1. Alcor says:

    Thank u.
    I needed to solve this problem!!!

  2. Appz says:

    I am getting an error after putting the command button into the update panel. It is saying that it cannot find the control for the trigger.
    Could this be because the command button is on one of the tabs in a tabcontainer which is placed inside an overall update panel?

  3. jinath says:

    what you mean by tabcontainer ? is it MultiView control? any way and why you use a command button ? try to use a normal asp.net button and call the export method

  4. solai says:

    1st solution is the best and the simple one.

    thanks a lot.. its working…!!!!

  5. Monty says:

    Ok, what If I have done all of the above and still get the error?

    Note: Perhaps an additional factor is that my Update Panel is sitting on a Panel for a modalPopupExtender.

    Here’s what I’ve done. Maybe its overkill:


    2) …ValidateRequest=”false” enableEventValidation=”false”%>

    3) Even in the Code-Behind
    ‘this line is for the Export to Excel control
    Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)

    4) I’ve even stuck this in the Web.Config

    5) and finally… here is the VB code behind for the button
    Protected Sub cmdExcelExport_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Response.AddHeader(“content-disposition”, “attachment;filename=FileName.xls”)
    Response.Charset = “”
    Response.ContentType = “application/vnd.xls”
    Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter
    Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
    End Sub

    End Sub

  6. Monty says:

    sorry I left out something, 4) should be:

    1) is intentionally blank – because that’s my first step 🙂

  7. Monty says:

    I just found it – ‘Postback’ not ‘Asynchronous’..

  8. jinath says:

    good that you found out the solution 🙂

  9. […] Jinath Blog, if you are using an UpdatePanel, you may get a […]

  10. AA says:

    Its not working finely

  11. Boby says:

    how to format the value which is going to Export to Excel from Gridview.??? please help me

  12. Omar says:

    Thank you.. This was exactly my problem and you resolved it.

  13. Thanks a lot this solutionj is working for me

  14. phaneesh says:

    thanks dude !!

  15. Travis says:

    Awesome Post Dude …. I was struggling with this problem for a while!

  16. Amol says:

    Thanx buddy u solved my problem

  17. Adeel Fakhar says:

    Superb job. There is also a very informative post for exporting gridview data to excel within an update panel that covers different case senarios. You can check this here at


  18. thiago says:

    very good solution !!

  19. shri says:

    very good man it worked for me

  20. MJ says:

    I am using an update panel on a master page and needed the a control within a child page to become the trigger. This is how I got my solution to work…

    In the Master page expose a public property
    public string TriggerId

    PostBackTrigger trigger = new PostBackTrigger();

    trigger.ControlID = value.ToString();
    In the page that uses the master set the property in the master page.
    protected void CreateMasterTrigger()
    ProjectMasterPageClass m = (ProjectMasterPageClass )Page.Master;
    m.TriggerId = lnkExport.UniqueID;

    There you go!

  21. Amar says:

    2st solution is the best and the simple one.
    thanks a lot.. its working…!!!!

  22. Angie says:

    thanks! it’s helped

  23. Geller says:

    Cool its helped me out

  24. Viral Patel says:

    Thanks a lot !!!

    This solution really helped me a lot !

  25. Peter says:

    Thank you veru much this helped me solve the problem of hours of looking for a solution on this problem

  26. Cassiano says:

    Thank you man!! I’ve been looking for this answer for hours… You are the only one a found with the clear solutions…

  27. Roulyo says:

    Thank’s for the fix !

  28. chris says:

    Thanks a lot on this sharing. It saves my time a lot!

  29. nicklibee says:

    I am facing the same problem in my project.

    I have a master page and iam using update panel.

    I have pasted the below code in master page
    Public WriteOnly Property TriggerId() As String
    Set(ByVal value As String)

    Dim trigger As New PostBackTrigger()

    trigger.ControlID = value.ToString()
    End Set
    End Property

    and on the .aspx page,wehre master page is used, iam writing the following fucntion
    Protected Sub CreateMasterTrigger()
    Dim m As SIDFIntranetMaster = DirectCast(Page.Master, SIDFIntranetMaster)
    m.TriggerId = btnExport.UniqueID
    End Sub

    and iam calling this function on the page load.
    still iam getting the same error.
    kindly help me

    thanks in advance

  30. Vivek Sharma says:

    Thank you so much for your solution….by putting my export button outside the update panel resolved my problem…..Thanxx a ton to you and keep it up

  31. Imteyaz Khan says:

    Thanks buddy,…It also helped be as i was struggling to using export link button in update panel and it was my requirement.

    I used first option to add trigger of the control.

  32. pankul says:

    thanks a lot it saved my whole day……

  33. miguelito says:

    Days working on this problem, solved in seconds!! Thanks, my friend

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s