63

I have a grid view on my page and I want to export it to the Excel Sheet, Below is the code I had written to do this task, here I am already passing the dataset to the method to bind the grid and btnExcelExport is the button which will export the Grid Content in to Excel Sheet :-

private void BindGridView(DataSet ds)
{
    if (ds.Tables.Count > 0)
    {
        if (ds.Tables[0].Rows.Count > 0)
        {
            GVUserReport.DataSource = ds;
            GVUserReport.DataBind();
            btnExcelExport.Visible = true;
        }
    }
}

protected void btnExcelExport_Click(object sender, EventArgs e)
{
    Response.Clear();
    Response.AddHeader("content-disposition","attachment;filename=FileName.xls");
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/vnd.xls";
    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    GVUserReport.RenderControl(htmlWrite);
    Response.Write(stringWrite.ToString());
    Response.End();
}

public override void VerifyRenderingInServerForm(Control control)
{
    return;
}

Now when I am debugging I found that the grid is binded sucessfully but when trying to export it to Excel, I'm getting this error:

"Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed."

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
Dharmendra Kumar Singh
  • 2,979
  • 10
  • 46
  • 63

10 Answers10

148

I fixed this issue. As I'm using UpdatePanel, I added below code in the Page_Load event of the page and it worked for me:

protected void Page_Load(object sender, EventArgs e) {
  ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
  scriptManager.RegisterPostBackControl(this.btnExcelExport);
  //Further code goes here....
}
StuartLC
  • 104,537
  • 17
  • 209
  • 285
Dharmendra Kumar Singh
  • 2,979
  • 10
  • 46
  • 63
  • @ShivaPareek it should work any way, but if it doesn't place a ScriptManagerProxy and use that! – Peter Jul 05 '13 at 11:03
  • I've used this approach and the whole page is reload. IsPostBackProperty = false which results in not firing the event for the export ? What may be the issue ? – Dimitar Tsonev Oct 02 '13 at 14:08
  • 1
    @KrishnaTiwari I added RegisterPostBackControl to Page_Load and PostBackTrigger to my UpdatePanel. The error disappeared but it now reloads my page. My button is inside UpdatePanel with Conditional mode & ChildrenAsTriggers... No Response.Write in the code behind. what to do? – nickornotto Jan 24 '14 at 16:21
  • Update Panel was the issue for me, I couldn't access the script manager, and I was able to remove update panel – Brent Aug 01 '14 at 17:31
  • Works for me, thank you. Is anyone has an explaination why this piece of code is required ? – AlexB Mar 02 '17 at 10:27
  • This is simplified vb syntax of above code. ScriptManager.GetCurrent(Page).RegisterPostBackControl([GrdFiles_controlname]) – noman zafar May 16 '20 at 10:33
  • +1 Works great. Also I recommend debugging to ensure that those lines are actually hit on execution. In our case we used `Page_PreRender` to register the control, but the method name was incorrectly spelt as `Pre_Render` so the method was never actually being invoked. A stupid mistake but was much harder to spot than it should have been as the ASP.Net error was deep within the business logic which misguided us. – EvilDr Nov 23 '21 at 11:58
  • I resolved this error by removing the Response.Write code from the code behind which was breaking the AJAX script http://dotnetdebug.net/2006/12/28/syswebformspagerequestmanagerparsererrorexception/ – nimblebit Nov 24 '22 at 13:05
19

In my case, the problem was caused by some Response.Write commands at Master Page of the website (code behind). They were there only for debugging purposes (that's not the best way, I know)...

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
Marcelo Luz
  • 500
  • 4
  • 7
  • had same problem.. some debug response write stopped update of cities dropdown after country selected. – Shaakir Aug 23 '17 at 12:55
10

I added the control to the Triggers tag in the update panel:

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

This way the exportLinkButton will trigger the UpdatePanel to update.
More info here.

Trisped
  • 5,705
  • 2
  • 45
  • 58
6

I had the same error, then I tried <asp:PostBackTrigger ControlID="xyz"/> instead of AsyncPostBackTrigger .This worked for me. It is because we don't want a partial postback.

6

1- Never use Response.Write.

2- I put the code below after create (not in Page_Load) a LinkButton (dynamically) and solved my problem:

ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
scriptManager.RegisterPostBackControl(lblbtndoc1);
StealthRT
  • 10,108
  • 40
  • 183
  • 342
Nuno Ribeiro
  • 2,487
  • 2
  • 26
  • 29
  • 1
    I suppose the better approach to ad-hoc response writes would be to enable trace on the page you wish to debug and use Trace.Write. – m-albert Feb 18 '16 at 18:10
5

For my VB.Net Friends -

Dim scriptManager As ScriptManager = scriptManager.GetCurrent(Me.Page)
scriptManager.RegisterPostBackControl(Me.YourButtonNameHere)
rayt
  • 194
  • 1
  • 8
3

Add this to you PageLoad and it will solve your problem:

ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
scriptManager.RegisterPostBackControl(this.lblbtndoc1);
Trisped
  • 5,705
  • 2
  • 45
  • 58
1

This worked for me too, but with an addition (below).

protected void Page_Load(object sender, EventArgs e) {
  ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
  scriptManager.RegisterPostBackControl(this.btnExcelExport);
  //Further code goes here....
}

I was registering a script on my button to click on another button after its event was finished processing. In order for it to work, I had to remove the other button from the Update Panel (just in case somebody faces the same problem).

Community
  • 1
  • 1
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
0

What worked for me was setting aspnet:MaxHttpCollectionKeys to a high value on appSettings tag on the inetpub VirtualDirectories\443\web.config file:

<configuration>
    <appSettings>
        <add key="aspnet:MaxHttpCollectionKeys" value="100000" />
    </appSettings>
</configuration>
felixy
  • 179
  • 1
  • 6
0

Before doing all this please check if you have given asp:LinkButton ID is same as asp:PostBackTrigger ControlID. If its not, the page going to through the same error