Script(s)

what I learn is what u c

AJAX – Export to Excel / Word

with 17 comments

If you are using Update Panel and try to Export gridview contents to EXCEL or Word, your asp.net page will throw nasty error. “Sys.WebForms.PageRequestManagerParserErrorException”

Its because AJAX & Response.write don’t go together.  By calling Response.Write() directly you are bypassing the normal rendering mechanism of ASP.NET controls. The bits you write are going straight out to the client without further processing (well, mostly…). This means that UpdatePanel can’t encode the data in its special format.

There are couple of ways to solve this problem.

1. Place the Export Button outside of Update Panel.

2. By Pass the Export Button using <Triggers>

 </ContentTemplate>
        <Triggers>
            <asp:PostBackTrigger ControlID=”cmdExport” />
        </Triggers>

</asp:UpdatePanel>
if your Export button is part of UserControl then specify usercontrolid:buttonid

 </ContentTemplate>
        <Triggers>
            <asp:PostBackTrigger ControlID=”uscSelCtl:cmdExport” />
        </Triggers>
    </asp:UpdatePanel>

More Reading…

Advertisement

Written by gchandra

January 8, 2008 at 10:12 am

17 Responses

Subscribe to comments with RSS.

  1. Thank you, just what i was looking for!

    petka

    May 1, 2008 at 5:59 pm

  2. “usercontrolid:buttonid” Elegant

    Nice =)…. thankss

    Pablogrind

    May 12, 2008 at 11:50 pm

  3. what if the user control is dynamically defined in the code behind? in the aspx page i have a placeholder. how would i access that user control button from within the update panel? is there a way to do this?

    vk

    July 15, 2008 at 9:09 am

  4. Thanks so much. You help me slove my problem. ^^

    chuti

    October 8, 2008 at 3:56 am

  5. When i use Export Button outside the update panel then i got this error

    The Controls collection cannot be modified because the control contains code blocks (i.e. ).

    and when i use trigger then i got this error

    A control with ID ‘UpdatePanel1:ddl_ExportGrid’ could not be found for the trigger in UpdatePanel ‘UpdatePanel1’

    I have UpdatePanel having id UpdatePanel1 that contains grid view and in grid i have a dropdown list having id ddl_ExportGrid.

    Please help….!

    Adeel Fakhar

    May 2, 2009 at 4:43 am

  6. Thx so much

    I search for a long time about this problem

    AkE

    July 21, 2009 at 4:59 am

  7. Excellent stuff, I searched for this one and left it, and now I found it.
    Thanks so much chandra

    Ram

    August 13, 2009 at 11:30 am

  8. Thanks man,…. thanks a lot… it solved my problem…

    shahid

    December 28, 2009 at 12:09 pm

  9. […] https://gchandra.wordpress.com/2008/01/08/ajax-export-to-excel-word-syswebformspagerequestmanagerpars… Subscribe to comments Comment | Trackback | Post Tags: Ajax, ASP .Net, Export to Excel, Export to Word, MS Visual Studio, Office Excel, Office Word, Visual Studio 2005 […]

  10. Try to use Spire.DataExport (http://www.e-iceblue.com/Introduce/data-export-for-net-intro.html) – it is c# data export component that supports data export into MS Excel,MS Word, HTML, XML, PDF, MS Access, DBF, SQL Script, SYLK, DIF, CSV ,MS Clipboard format.

    rlejason

    August 9, 2010 at 12:23 am

  11. thanks you so much. this is what i am looking for since last 6 hours. thanks a lot

    Brindesh

    January 28, 2011 at 5:07 am

  12. I am unable to export to excel when an update panel has server side controls like dropdown,textbox etc.. can any one resolve this

    lavanya

    March 28, 2011 at 9:32 am

  13. you made my day……………

    jagadeesh

    July 6, 2011 at 12:08 pm

  14. Thank you so much gchandra, this really helped me. Since we use a Master Page it was seemingly becoming a challenge to get the Export to excel function (which contains response.write) to get to work.

    Using your method of declaring the Triggers seemed not to work as i put only the control name there, while the control belonged to a content page. then following your advise for the user control, we used:
    contentpalceholderId:control Id and it worked great! thanks a lot.

    and to complete my reply, we also had to replace the Response.End with HttpContext.Current.ApplicationInstance.CompleteRequest(); to avoid the exception occurring due to incomplete .NET page cycle. ( ref: http://support.microsoft.com/kb/312629/EN-US/ )

    Saikat

    September 10, 2011 at 12:20 pm

  15. Thank you so much gchandra, this really helped me. Since we use a Master Page it was seemingly becoming a challenge to get the Export to excel function (which contains response.write) to get to work.

    Using your method of declaring the Triggers seemed not to work as i put only the control name there, while the control belonged to a content page. then following your advise for the user control, we used:
    contentpalceholderId:control Id and it worked great! thanks a lot.

    < asp:UpdatePanel ID=”updPNLCntPlHldr1″ runat=”server” UpdateMode=”Conditional” >
    < ContentTemplate >
    < asp:ContentPlaceHolder ID=”MainFrame” runat=”server” />
    </ ContentTemplate >
    < Triggers >
    < asp:PostBackTrigger ControlID=”MainFrame:ibtnExportData” />
    </ Triggers >
    </ asp:UpdatePanel >

    and to complete my reply, we also had to replace the Response.End with HttpContext.Current.ApplicationInstance.CompleteRequest(); to avoid the exception occurring due to incomplete .NET page cycle. ( ref: http://support.microsoft.com/kb/312629/EN-US/ )

    Saikat

    September 10, 2011 at 12:26 pm


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 )

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

%d bloggers like this: