tags:

views:

32

answers:

1

Hi I am trying Export data to excel sheet from GridView but having this error.

RegisterForEventValidation can only be called during Render();

Here is my code

        Dim attachment As String
        attachment = "attachment; filename=Contacts.xls"
        Response.ClearContent()
        Response.AddHeader("content-disposition", attachment)
        Response.ContentType = "application/ms-excel"
        Dim myStringWriter As New IO.StringWriter
        Dim myhtmlStringWriter As New HtmlTextWriter(myStringWriter)
        GridView1.RenderControl(myhtmlStringWriter)
        Response.Write(myStringWriter.ToString)
        Response.End()

Thanks

+1  A: 

You can do this in the web.config file but in this case the eventValidation will be turned off for all the pages.

or you can do this in the Page directive which will turn off the validation for a single page.

<%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true"

CodeFile="ExportGridView.aspx.cs" Inherits="ExportGridView" %>

Talha Bin Shakir