Control must be placed inside a form tag with runat=server in NET 2.0
GridView Export to Excel ProblemsThis guy researched why simple methods like export datagrid to Excel don't work with GridViews or other controls. I have also stumbled on this stupid error when trying to use RenderControl to get the output of a UserControl.
Apparently, the entire problem lies with the
Page.VerifyRenderingInServerForm Method which throws an exception if the page is not currently in the render phase of page processing, and inside the <form runat=server> tags.
Luckily it can be overridden.
This is the actual code for the method in the Page control in NET 2.0. Just override the hell out of it.
public virtual void VerifyRenderingInServerForm(Control control)
{
if (this.Context == null || base.DesignMode) return;
if (control == null)
{
throw new ArgumentNullException("control");
}
if (!this._inOnFormRender && !this.IsCallback)
{
throw new HttpException(System.Web.SR.GetString("ControlRenderedOutsideServerForm", new object[] {
control.ClientID,
control.GetType().Name
}));
}
}
