Solved

ASP.NET VB Export to Excel Exception

Posted on 2010-08-24
1
264 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

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…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

739 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