GridView to Excel error

D

David

Using ASP.Net 2.0 and I have identical code for exporting a GridView to
Excel in 2 web sites. One works fine and the 2nd one gives me the error
"RegisterForEventValidation can only be called during Render();"

This error occurs on the line that reads "gv.RenderControl(hw)"

Below is my code. Can anybody give me a clue? Thanks.

Sub SendToExcel(ByVal Source As Object, ByVal E As EventArgs)

Dim gv As GridView = SearchResultsGridView

If gv.Rows.Count.ToString + 1 < 65536 Then


gv.AllowPaging = False

gv.AllowSorting = False

If txtSearchText.Text <> "" Then

SqlvwFileSearch.SelectCommand = txtSearchText.Text

End If

gv.DataBind()


Dim tw As New StringWriter()

Dim hw As New System.Web.UI.HtmlTextWriter(tw)

Response.Clear()

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

Response.AddHeader("content-disposition", "attachment;filename=search.xls")

Response.Charset = ""

EnableViewState = False

gv.RenderControl(hw)

Response.Write(tw.ToString())

Response.End()

'gv.AllowPaging = True

gv.AllowSorting = True

gv.DataBind()

lblErrorMsg.Text = ""

Else

lblErrorMsg.Text = "Too many rows - Export to Excel not possible"

End If

End Sub


Public Overrides Sub VerifyRenderingInServerForm(ByVal control As
System.Web.UI.Control)

End Sub

David
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top