Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
2.38/5 (5 votes)
See more:
I am gettig error as below when export datagrid to excel.

Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.

see my code :
Posted
Updated 9-May-12 3:29am
v2

As I thought, your code behind confirmed it that you indeed are exporting your grid.

The above exception occurs when one tries to export a GridView control to Word, Excel, PDF, CSV or any other formats. Here the .net compiler thinks that the control is not added to the form and is rendered, hence it throws this error even if your GridView control is inside a form with runat = “server”.

You have to tell the compiler that the control is rendered explicitly by overriding the VerifyRenderingInServerForm event.
Like:
C#
public override void VerifyRenderingInServerForm(Control control)
{
   //
}


This will confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time
Look at the event detail here: Page.VerifyRenderingInServerForm Method[^]
 
Share this answer
 
Comments
[no name] 30-Mar-13 5:15am    
i tried both solution but still i have a same problem......

plz help me
im wating for your help.....
Maria Norbert 18-Oct-14 8:32am    
Thank you Sandeep Mewara,
This code has solved this problem for me.
I have encountered similar problems a number of times and it occurs when you have EnablePaging or EnableSorting set to true.
The solution would be to disable sorting and/or paging before the export and reenable them after the export.
 
Share this answer
 
Comments
[no name] 30-Mar-13 5:14am    
i tried both solution but still i have a same problem......

plz help me
im wating for your help.....
Member 9861478 27-Jun-13 23:51pm    
it's working for me!!
Hanna Chojnicka 18-Jan-16 7:24am    
It's working! You must write:
GridView2.AllowSorting = false;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900