Export gridview to excel within an UpdatePanel in asp.net using c#

In this asp.net tutorial we will learn how to export gridview to excel within an UpdatePanel. Last couple of days I was working over a report in which there is a requirement to export gridview data to excel, I thought that it’s quite easy as I have done it before but I forgot that now this time the gridview is within ajax UpdatePanel. When I started to export then I found couple of errors like System.WebForms.PageRequestManagerParserErrorException exception etc.

Any how, when I search this over internet then I found couple of solutions that I will discuss on this post too but none of them worked in my case but still I become successful to achieve my target without removing update panel. Let’s see how to export gridview to excel within an update panel.

Before I start please find the code to export gridview to excel. Actually I have written a post to export gridview to excel without UpdatePanel so please use that code to export data. Actually that code will be responsible to export the data and in this current post i am just telling how to get rid of the errors that a programmer can face during exporting gridview to excel within an update panel. So let’s start


There are three solutions for export to work within an update panel; it’s depending upon your case which solution to be followed.

1) Remove UpdatePanel tag

The first solution is to simply remove the updatePanel tag but still there is a question that if you want to remove update panel tag then what are you doing here :) So to better understand let’s consider you have an asp button and you want to export the gridview data by clicking on it, so if you want to successfully export the data please close the UpdatePanel tag before the asp button and then again after asp button please insert the updatePanel and then close UpdatePanel in the end of the page.

2) Use PostBackTrigger
The second solution is to use PostBackTrigger before closing tag of </asp:UpdatePanel> and give asp button’s(That you want to use to export gridview data to excel) ID to the ControlID of asp:PostBackTrigger like this


………………………………………………………………
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>

The whole aspx code will come here

<asp:Button ID="btn_export" runat="server" Text="Export to excel" onclick="btn_export_Click" />


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

……………………………………………………………

It will work and you will achieve what you want but there is a case scenario that I had, please have a look over below image

Export gridview to excel within an updatePanel in asp.net using c#

Yeah in this case there is a dropdown list and I wanted this dropdown list to export the data but this dropdown list made me mad as every time it gives me error. First of all I attached the export to excel code to the OnSelectedIndexChanged event of asp dropdown list but I got this 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 '<style> .text { mso-'



Then I again put the code of exporting gridview data to the asp button’s onClick event and tried to call the onClick event of asp button over OnSelectedIndexChanged event of asp dropdown list but I got this same 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 '<style> .text { mso-'.


Then I tried to give the asp dropdown list’s ID to the ControlID of <asp:PostBackTrigger> but I got this error

A control with ID 'ddl_ExportGrid' could not be found for the trigger in UpdatePanel 'UpdatePanel1'.

I tried a lot to solve this error by providing lot of options to the ControlID of <asp:PostBackTrigger> such as

<asp:PostBackTrigger ControlID="UpdatePanel1:ddl_ExportGrid" />

<asp:PostBackTrigger ControlID="GridView1:ddl_ExportGrid" />

But still got “could not be found for the trigger” error.


Then I found a solution and its work 100%. The solution is to call a javascript function over client side onChange event of asp dropdown list and that's it. Please have a look over javascript function that you will call over onChange event of asp dropdown list.

function ExportGridviewtoExcel()

{
__doPostBack("<%=btn_export.UniqueID %>",'');
}

Note:- Before doPostBack() function there are two underscores(_)

I am using doPostBack built-in javascript function and in this function I am providing the asp button’s ID as a first argument to it. Now the code for asp dropdown list will be like this

<asp:DropDownList ID="ddl_ExportGrid" runat="server" style="vertical-align:middle;" onchange="ExportGridviewtoExcel();">
<asp:ListItem Value="0" Text="Select a format" />
<asp:ListItem Value="1" Text="Export to Excel" />
</asp:DropDownList> Export

Also please make the asp button invisible so that user can’t see it but please don’t remove it as the export gridview to excel code is written on asp button’s onClick() event and by using javascript function over onChange event of asp dropdown list actually we are calling this asp button. So in the end the code will be

……………………………………………………………………………………
<asp:Button ID="btn_export" runat="server" Text="Export to excel" onclick="btn_export_Click" Visible="false" />

</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btn_export" />
</Triggers>

</asp:UpdatePanel>


……………………………………………………………………………………

I try my level best to explain how to export gridview data to excel within an UpdatePanel and I hope these solutions will work for you.

Happy Coding…… Keep Coding….!

9 comments:

  • Anonymous
     

    Excellent article. It solved my biggest problem with simple solution.

    Arun, India

  • Anonymous
     

    Great article! Very clear and very clever! --Aspen, USA

  • Valeriano Simeone
     

    hi,

    and if te updatepanel is in a MaterPage?

  • ravinder
     

    U r great yar ..Excellent Work

  • ravinder
     

    excellnt Yar.............. u r great..thanx friend

  • Saikrishna
     

    Good this Article helped me a lot!!!!!

    U rock dude..

  • Anonymous
     

    You have a solution here, but you haven't explained the mechanics of the issue here.

  • Anonymous
     

    Good.. It worked out well..

  • Anonymous
     

    Good.. It worked out well..