Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ASP.NET VB Export to Excel Exception

Posted on 2010-08-24
1
Medium Priority
?
277 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

885 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