.NET (290) administrative (42) Ajax (42) AngularJS (1) ASP.NET (144) bicycle (2) books (179) browser (8) C# (128) cars (1) chess (27) CodePlex (10) Coma (8) database (46) deployment (3) Entity Framework (2) essay (109) flash/shockwave (2) flex (1) food (3) friend (2) game (20) idea (5) IIS (8) javascript (81) LInQ (2) Linux (6) management (4) manga (43) misc (664) mobile (1) movies (88) MsAccess (1) murder (2) music (65) mysql (1) news (98) permanent (1) personal (66) PHP (1) physics (2) picture (309) places (12) politics (13) programming (495) rant (118) religion (3) science (40) Sharepoint (3) software (57) T4 (2) technology (11) Test Driven Development (4) translation (2) VB (2) video (100) Visual Studio (44) web design (45) Windows API (8) Windows Forms (3) Windows Server (4) WPF/Silverlight (63) XML (11)

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.

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

4 comments:

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...

Hi,

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.