Welcome

Hello, Welcome to my blog. If you like feel free to refer others

Thursday 2 February 2012

Some problems and their solutions

In my previous post ASP.Net GridView Export only selected columns to Excel Sheet. I have described how to export gridview selected column into excel sheet. 

Now I will describe some problem during this export.

Problem 1:

RegisterForEventValidation can only be called during Render()

Server Error in 'ASP.Net' Application.

RegisterForEventValidation can only be called during Render();
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: RegisterForEventValidation can only be called during Render();
 
Cause
The above error occurs when you try to render a control to Response. Generally I got this when I exported GridView to Excel, Word, PDF or CSV formats. The ASP.Net compiler issues the above Exception since it feels the Event is invalid.
Solution
The solution is quite simple you need to notify ASP.Net that not to validate the event by setting the EnableEventValidation flag to FALSE.
You can set it in the Web.Config in the following way
<pages enableEventValidation ="false"></pages>
 
This will apply to all the pages in your website. Else you can also set it in the @Page Directive of the page on which you are experiencing the above error.
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation = "false"
 
The above solution helped me resolve my issue. If anyone has something else that helped him please share.

Problem 2:

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


Server Error in '/ASP.Net' Application.

Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
 
Cause
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”. 
Solution
1. Tell the compiler that the control is rendered explicitly by overriding the VerifyRenderingInServerForm event. You can do that by adding the event to tha code behind file. Refer below
  C#
public override void VerifyRenderingInServerForm(Control control)

{

    /* Verifies that the control is rendered */

}
 
If anyone has some other solution please share. If you have any other exception do submit it to us using the Contact page.

Problem 3 :
Exception -RegisterForEventValidation can only be called during Render();
Server Error in 'ASP.Net' Application.

RegisterForEventValidation can only be called during Render();
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: RegisterForEventValidation can only be called during Render();
 

  Cause
The above error occurs when you try to render a control to Response. Generally I got this when I exported GridView to Excel, Word, PDF or CSV formats. The ASP.Net compiler issues the above Exception since it feels the Event is invalid.

  Solution
The solution is quite simple you need to notify ASP.Net that not to validate the event by setting the EnableEventValidation flag to FALSE.
You can set it in the Web.Config in the following way
<pages enableEventValidation ="false"></pages>
 
This will apply to all the pages in your website. Else you can also set it in the @Page Directive of the page on which you are experiencing the above error.
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation = "false"
 
The above solution helped me resolve my issue. If anyone has something else that helped him please share.


Happy learning....... :)
 

No comments:

Post a Comment