Export To Excel Error

P

polabear

I'm trying to use the below code to export a gridview to excel, btu I get the
following error:

RegisterForEventValidation can only be called during Render();

Any ideas are appreciated.


Export code after the grid has been databound:

Response.Clear()
Response.Buffer = True
Response.AddHeader("Content-Disposition",
"filename=C:\ExcelTestFile.xls")
Response.Charset = ""
Dim stringWriter As StringWriter = New StringWriter
Dim htmlWriter As HtmlTextWriter = New HtmlTextWriter(stringWriter)
Me.grdModels.RenderControl(htmlWriter)
Response.Write(stringWriter.ToString())
Response.ContentType = "application/vnd.ms-excel"
Response.End()

Since I got an error :

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

I added this code:


Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)

End Sub

polarbear
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top