Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP.NET VB Export to Excel Exception

Posted on 2010-08-24
1
Medium Priority
?
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

722 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