Friday, August 18, 2006

Control must be placed inside a form tag with runat=server in NET 2.0

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[] { 


Optiontrader1138 said...

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't add a custom control to the destination panel because of this error checking.

Overriding the hell out of it worked like a charm!

Andrey Z said...

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

Sarwa said...


Sorry I dont understand what is System.Web.SR.GetString... it is showing error for me and "if (!this._inOnFormRender && !this.IsCallback)" what i have to place instead of "_inOnFormRender " and "IsCallback".

where we need to place this method ... in usercontrol or in aspx.cs

Siderite said...

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