GridView to Excel - prevents 

"RegisterForEventValidation can only be called during Render()"


'---------------------------------------------------------

'aspx

'---------------------------------------------------------

<%@ Page Language="vb" AutoEventWireup="true" enableEventValidation ="false" CodeBehind="AssayList.aspx.vb" Inherits="TestRequest.AssayList" %>

  

'---------------------------------------------------------

'aspx.vb

'---------------------------------------------------------

 Protected Sub ibExcelExport_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibExcelExport.Click

        ExportToExcel("MyReport.xls", Me.gv1)

    End Sub

   

Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)

 

    End Sub

   

Public Sub ExportToExcel(ByVal strFileName As String, ByVal dg As GridView)

        Response.Clear()

        Response.Buffer = True

        Response.ContentType = "application/vnd.ms-excel"

        Response.Charset = ""

        Me.EnableViewState = False

        Dim oStringWriter As New System.IO.StringWriter

        Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)

 

        dg.RenderControl(oHtmlTextWriter)

 

        Response.Write(oStringWriter.ToString())

        Response.[End]()

 

    End Sub