GridView Export to Excel Problems

This 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. Here is the bug posting at Microsoft and their suggested solutions. Microsoft removed the page, so I can't show it to you.

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
}));
}
}

Comments

Siderite

That is the actual .NET method. You only override it with an empty method.

Siderite

Sarwa

Hi, Sorry I dont understand what is System.Web.SR.GetString... it is showing error for me and &quot;if (!this._inOnFormRender &amp;&amp; !this.IsCallback)&quot; what i have to place instead of &quot;_inOnFormRender &quot; and &quot;IsCallback&quot;. where we need to place this method ... in usercontrol or in aspx.cs

Sarwa

Andrey Z

Great thanks for you post. I search a long time solution to render user control with server elements that generating post back. It&#39;s really work after override method.

Andrey Z

Optiontrader1138

Thanks for posting this. I was using a hidden button triggered by javascript to allow two AJAX updatepanels to talk to each other. However, I couldn&#39;t add a custom control to the destination panel because of this error checking. Overriding the hell out of it worked like a charm!

Optiontrader1138

Post a comment