Phil
Phil

Reputation: 1841

exporting gridview contents to excel spreadsheet

I have a gridvidew (GV2). I want the user to be able to export the contents of this gridview to an excel spreadsheet for offline processing.

Here is my subroutine:

Protected Sub ExcelButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ExcelButton.Click
        Response.ContentType = "application/vnd.ms-excel"
        Response.Charset = ""
        Me.EnableViewState = False
        Dim stringWriter As New System.IO.StringWriter()
        Dim htmlWriter As New System.Web.UI.HtmlTextWriter(stringWriter)
        GV2.RenderControl(htmlWriter)
        Response.Write(stringWriter.ToString())
        Response.End()
    End Sub

On clicking the ExcelButton I get the error message:

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

The control GV2 is in fact inside:

 <form id="form1" runat="server"></form>

Upvotes: 1

Views: 2113

Answers (1)

o.k.w
o.k.w

Reputation: 25810

There's a 'story' behind this error message. I'll go straight to one of the solutions.

Add this to the code-behind of the ASPX file:

Public Overrides Sub VerifyRenderingInServerForm(control As Control)

End Sub

More info here:

How to export GridView to Word using ASP.NET 2.0 and VB
CodeSnip: Exporting GridView to Excel

Upvotes: 2

Related Questions