Export to Excel - ASP.net 2.0

N

Not Me

Hi,

I'm trying to export from a gridview control, to an excel file using
code intended for a datagrid control (it's all over the web, can post if
requested)

I get the error.

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

This error is similar to that experienced when the datagrid had
server-side controls such as edit buttons and the like, but my gridview
is very plain, just displaying text fields (and it is of course inside a
form tag with runat=server).

Are there any tweaks that could be made to allow the code to work? or is
a completely different method the only way?

Cheers,
Chris
 
C

Cowboy \(Gregory A. Beamer\)

Look at the tags. Your's will look something like this:

</form>
<asp:GridView id="gvSearch" runat="server" />

Just make sure you move the GridView tag up inside of the </form> tag. If
there are other elements that are outside the tag, that also have to be
above the GridView, pull them up as well.

This normally happens most often when you throw something at the end of a
page after you have hand edited the tags in HTML view at least once. It is
easy to fix.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***********************************************
Think Outside the Box!
***********************************************
 
N

Not Me

Cowboy said:
Look at the tags. Your's will look something like this:

</form>
<asp:GridView id="gvSearch" runat="server" />

Just make sure you move the GridView tag up inside of the </form> tag. If
there are other elements that are outside the tag, that also have to be
above the GridView, pull them up as well.

Thanks, but that it not the case with my code, the gridview (in fact
pretty much the whole page) is within the <form> tags.

Any other ideas? I'm guessing from your response that there's no reason
for this not to work under asp.net 2.0?

Cheers,
Chris
 
P

PL

Any other ideas? I'm guessing from your response that there's no reason for this not to work under asp.net 2.0?

Make sure all tags are properly closed, validate the html.

Strange results can happen if there are unclosed tags within the form ruant=server
and inside the datagrid/gridview.

PL.
 
N

Not Me

Make sure all tags are properly closed, validate the html.

Strange results can happen if there are unclosed tags within the form ruant=server
and inside the datagrid/gridview.

Bizarrely I had a 'runat=server' in the <head> tags :) however removing
that did not solve the problem, and I couldn't find any other issues.

I apologise for the code-dump but if it helps this is the page causing
issues...(bigsearchresults.aspx)


**********************************************
<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="BigSearchResults.aspx.vb" Inherits="BigSearchResults" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Results!</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btExport" runat="server" OnClick="btExport_Click"
Text="Export" Style="position: relative; z-index: 100;" />

<asp:SqlDataSource ID="dsSearch" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="aspBigSearch" SelectCommandType="StoredProcedure">

<SelectParameters>
<asp:SessionParameter SessionField="ac" Name="ac" Type="String" />
<asp:SessionParameter SessionField="ad" Name="ad" Type="String" />
<asp:SessionParameter SessionField="cd" Name="cd" Type="String" />
<asp:SessionParameter SessionField="course" Name="course"
Type="String" ConvertEmptyStringToNull="false" />
<asp:SessionParameter SessionField="yearfrom" Name="gradyearfrom"
Type="Int32" DefaultValue="0" />
<asp:SessionParameter SessionField="yearto" Name="gradyearto"
Type="Int32" DefaultValue="0" />
</SelectParameters>
</asp:SqlDataSource>

<asp:GridView ID="gvSearch" runat="server" DataSourceID="dsSearch"
BackColor="Silver" EmptyDataText="Sorry, your search has come up empty!"
AllowSorting="false">
</asp:GridView>
</div>
</form>
</body>
</html>
*******************************************

the codebehind bigsearchresults.vb is thus:

*******************************************
Partial Class BigSearchResults Inherits System.Web.UI.Page

Protected Sub btExport_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btExport.Click

Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Me.EnableViewState = False

Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)

gvSearch.RenderControl(hw)
Response.Write(tw.ToString())
Response.End()

End Sub
****************************

Really apprecaite any help!


Cheers,
Chris
 
G

Guest

Alternatively, you could fake a form in the HtmlTextWriter, like this:

Protected Sub btExport_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btExport.Click

Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Me.EnableViewState = False

Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)

Dim frm As HtmlForm = new HtmlForm()
Me.Controls.Add(frm)
frm.Controls.Add(gvSearch)

frm.RenderControl(hw)

Response.Write(tw.ToString())
Response.End()

End Sub
 
G

Guest

The gridview control is being asked to render itself inside an empty
htmltextwriter. Thus it is not inside a <form runat="server"...> tag.

Try changing your btExport method to this:

Protected Sub btExport_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btExport.Click
Response.ContentType = "application/vnd.ms-excel"
btExport.Visible = False
End Sub
 
N

Not Me

Alternatively, you could fake a form in the HtmlTextWriter, like this:
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
Dim frm As HtmlForm = new HtmlForm()
Me.Controls.Add(frm)
frm.Controls.Add(gvSearch)
frm.RenderControl(hw)
Response.Write(tw.ToString())
Response.End()

Genius!

That worked a treat, though I'm not sure why? - is it because I declare
the gridView inside the .aspx page that means it's within the <form>
tags, but things declared in the .vb page are outside of it? I'll have
to keep that in mind for future use.

Much appreciated, thank you!
Chris
 
G

Guest

I'm getting the same error and have tried all the suggestions from the
replies to this but to no avail. Here is my situation.

Gridview on page, page contains a master page. I have tried removing the
MasterPage and the RenderControl still doesn't work.

The following code
Dim stringWrite As New System.IO.StringWriter
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
grdQueryResults.RenderControl(htmlWrite)

will yield this error:
Control 'grdQueryResults' of type 'GridView' must be placed inside a form
tag with runat=server

I DEFINATELY have the GridView in a properly formed <Form> tag.
--------

If I use the method described by faking the form tag I get this error:
RegisterForEventValidation can only be called during Render();
-----

Also, if I use the method described on several other sites by adding an empty
Public Overrides Sub VerifyRenderingInServerForm
I get the same
RegisterForEventValidation can only be called during Render();
error

Any thoughts on this as this is driving me CRAZY. The only alternative
which I find not really acceptable is to use a DataGrid when exporting is
required. The RenderControl method on the DataGrid works as expected.

Thanks
-Chris
 
C

collcoll1

I set allowsorting = false in my gridview and the export to excel works
now, but I have to have sorting. Is there any other option?
 
B

bene_1979

*I set allowsorting = false in my gridview and the export to excel
works
now, but I have to have sorting. Is there any other option? *


Try to use 2 Grid views. One for the main page(with sorting enabled)
and one for exporting to excel(without sorting).
 
Joined
Jun 6, 2007
Messages
1
Reaction score
0
Hi,

I am also face the same problem... for this i just replace the Gridview with Datagrid...now it works fine... you also try the same...

:tea: Cheers,
Venis - MCP,MCAD

Not Me said:
Hi,

I'm trying to export from a gridview control, to an excel file using
code intended for a datagrid control (it's all over the web, can post if
requested)

I get the error.

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

This error is similar to that experienced when the datagrid had
server-side controls such as edit buttons and the like, but my gridview
is very plain, just displaying text fields (and it is of course inside a
form tag with runat=server).

Are there any tweaks that could be made to allow the code to work? or is
a completely different method the only way?

Cheers,
Chris
 
Last edited:
Joined
Jun 12, 2008
Messages
1
Reaction score
0
Adding the following code helped me


Code:
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)

    End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Export To Excel 1
export to excel 1
Export GridView to Excel Error 3
export to excel 0
Export To Excel Error 0
export gridview to excel format error 6
Export To Excel 16
I want to Display Excel As HTML In js 2

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top