RegisterForEventValidation can only be called during Render()[SOLVED]

Today i do my routine by developing / testing / and debuggig and found error on my web page :

RegisterForEventValidation can only be called during Render();

The code actually for get html code from server side inside panel ...

 var sb = new StringBuilder();
 Panel1.RenderControl(new HtmlTextWriter(new StringWriter(sb)));
 string htmlToConvert = sb.ToString();

So i do some googling and found the causes of this error occur..

The Causes

occurs when you try to render a control to Response. Generally some people 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 Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
 EnableEventValidation="false"
CodeBehind="PingInASPdotNet.aspx.cs" Inherits="BlogExample.PingInASPdotNet" %>

The above solution helped me solved my issue.Please share if you have better solution than this. Thanks.


By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

Popular posts from this blog

Example to disable save as certain file type in SSRS Report Viewer

Phone book application example - Java

Modal Popup Message Box ASP.NEt C# Example