Please enable Javascript for better experience...
Error:RegisterForEventValidation can only be called during Render
By Rahul Kumar Jha | Jan 15, 2015 | In Tips | Total Views [ 4550 ]
Taged In
(1 Like)

Sometime I get "RegisterForEventValidation can only be called during Render" error or "Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server" error while implementing export to excel functionality in my application. Let's see what is it all about.

The Problem

I was implementing export to excel functionality in my application. I had a GridView and was using this to get data into my export to excel method. Suddenly I got this error - "Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server". It was shocking because my grid was already in <form> tag.

    protected void ButtonExport_Click(object sender, EventArgs e)
            string.Format("attachment;filename={0}.xls", "Order"));
        Response.Charset = "";
        Response.ContentType = "application/vnd.xls";

        StringWriter stringWrite = new StringWriter();
        HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

My code was throwing rendering error. It was throwing error at "GridView1.RenderControl(htmlWrite);" line.

A quick solution

Simply add these line of code in your page and you can continue with your task without any hassle.

        public override void VerifyRenderingInServerForm(Control control)
            //Leave blank

Add EnableEventValidation="false" in page directive.

Hope it can help you.

Share this

About the Author

Rahul Kumar Jha
Rahul Kumar Jha
Founder, Developer

Public profile: user/profile/99900001

Has working experience in different phases of Software Development Life Cycle (SDLC) in CMS, Gaming, Health Care and Financial Services domain using Agile pattern. Working experience in Design patterns, ASP.NET, MVC, ANGULAR, ANGULAR JS, Windows application, WCF, ADO.NET, SQL Server and Test Driven Development (TDD) environment with JQuery, JavaScript, N-Unit, Entity Frameworks, LINQ, Code Refactoring and Business Objects Models.

User's Comments

Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID