Solved

ASP.NET VB Export to Excel Exception

Posted on 2010-08-24
1
255 Views
Last Modified: 2012-05-10
When trying to use the following export to excel in my button.click event:  Any help ??? (Please don't send me to another tutorial.  I've been to about 25 of them already).  Note that the grid is in the content area page and I'm using Master Pages.  The grid renders correctly and populates the data correctly from my datasource

Response.Clear()

        ' Set the type and filename   
        Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
        Response.Charset = ""
        Response.ContentType = "application/vnd.xls"

        ' Add the HTML from the GridView to a StringWriter so we can write it out later   
        Dim sw As System.IO.StringWriter = New System.IO.StringWriter
        Dim hw As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(sw)
        EmployeeGrid.RenderControl(hw)

        ' Write out the data   
        Response.Write(sw.ToString)
        Response.End()

Open in new window


I get this exception on the RenderControl call:

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

Below is my grid in asp (which loads perfectly)

 <asp:GridView ID="EmployeeGrid" runat="server" AutoGenerateColumns="False" 
        DataSourceID="ObjectDataSource1" 
        CssClass="mGrid"
        AlternatingRowStyle-CssClass="alt" >
        <Columns>
            <asp:BoundField DataField="LastName" HeaderText="LastName" 
                SortExpression="LastName" />
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
                SortExpression="FirstName" />
            <asp:BoundField DataField="EmailAddress" HeaderText="EmailAddress" 
                SortExpression="EmailAddress" NullDisplayText="---" DataFormatString="<a href=mailto:{0}>{0}</a>" HtmlEncodeFormatString="false"/>
            <asp:BoundField DataField="Location" HeaderText="Location" 
                SortExpression="Location" />
            <asp:BoundField DataField="JobTitle" HeaderText="JobTitle" 
                SortExpression="JobTitle" />
        </Columns>

<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
    </asp:GridView> 

Open in new window

0
Comment
Question by:rwdMike
1 Comment
 

Accepted Solution

by:
rwdMike earned 0 total points
ID: 33513536
Ok, well, figured it out.  The example I used didn't include the need to override VerifyRenderinginServerForm

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
    End Sub
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now