Export Gridview to excel inside an update panel
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.
”Sys.WebForms.PageRequestManagerParserErrorException“
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:-
<Triggers>
<asp:PostBackTrigger ControlID=”btnExcel” />
</Triggers>
2) or else you can move your excel export button out side the update panel






Thank u.
I needed to solve this problem!!!
Hi,
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?
hi
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
1st solution is the best and the simple one.
thanks a lot.. its working…!!!!
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:
1)
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.Clear()
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)
Me.gvHedis.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString)
Response.End()
End Sub
End Sub
sorry I left out something, 4) should be:
1) is intentionally blank – because that’s my first step
I just found it – ‘Postback’ not ‘Asynchronous’..
good that you found out the solution
[...] Jinath Blog, if you are using an UpdatePanel, you may get a [...]
Export GridView to Excel within an UpdatePanel by JohnnyCoder said this on Saturday, July 26, 2008 at 7:33 am |
Its not working finely
how to format the value which is going to Export to Excel from Gridview.??? please help me
Thank you.. This was exactly my problem and you resolved it.
Thank you……
Excellent
Thanks a lot this solutionj is working for me
thanks dude !!
Awesome Post Dude …. I was struggling with this problem for a while!
Thanx buddy u solved my problem
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
http://nice-tutorials.blogspot.com/2009/06/export-gridview-to-excel-within-update.html
very good solution !!
very good man it worked for me
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
{
set
{
AjaxMasterUpdatePanel.Triggers.Clear();
PostBackTrigger trigger = new PostBackTrigger();
trigger.ControlID = value.ToString();
AjaxMasterUpdatePanel.Triggers.Add(trigger);
}
}
++++++++++++++++++++++++++++++++++++++++++++++++
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!
nice work MJ
2st solution is the best and the simple one.
thanks a lot.. its working…!!!!