In the days of asp.net 1.1 when using DataGrid and exporting to Excel i never came across this error since there was no master pages etc..
But at the same time having master pages makes life easier and at the same time some troubles.
One of them is when using Ajax with GridView with a functionality to export to Excel.
The error you get is "Control 'ctl00_ContentPlaceHolder1_gdViewExport' of type 'GridView' must be placed inside a form tag with runat=server" when clicking on your export button.
After nearly pulling my head off i came across some sort of hint that when you have your
GridViewToExport.RenderControl (HtmlTextWriter) in your code
the page won't know what to do unless you call this method VerifyRenderingInServerForm method click on the link to
read more
. It simply overrides the Render method to ensure that this control is nested in an HtmlForm server control .Anyway after wrapping the method as suggested it worked all well.
But now here comes another trouble when using UpdatePanel in you Gridview and cilck your export button you get error :
inside UpdatePanel control, it gives this error:
Sys.WebForms.PageRequestManagerParserErrorException: The message received from teh 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 '<div> <table cells'.
And i'm sure you'd say argggg!
But doing EnableEventValidation="false" could solve your problem but in my case i need to use validation on my page.
But i knew some AsyncPostBackTrigger and PostBackTrigger exists after the Triggers even in the UpdatePanel So what i did was do :
<asp:PostBackTrigger ControlID="imgexcel" />
setting the ControlID to the control ID of my asp:imagebutton control
And bingo that did the trick