469,331 Members | 5,644 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,331 developers. It's quick & easy.

Export to Excel - ASP.net 2.0

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
Nov 19 '05 #1
11 5459
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!
***********************************************
"Not Me" <no****@zxy.blah.org> wrote in message
news:dl**********@ucsnew1.ncl.ac.uk...
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

Nov 19 '05 #2
Cowboy (Gregory A. Beamer) wrote:
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
Nov 19 '05 #3
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.
Nov 19 '05 #4
PL wrote:
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.


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

Nov 20 '05 #5
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
"Not Me" wrote:
PL wrote:
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.


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

Nov 20 '05 #6
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


"Not Me" wrote:
PL wrote:
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.


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

Nov 20 '05 #7
JGordon wrote:
"Not Me" wrote:
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()
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
Nov 20 '05 #8
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
"Not Me" wrote:
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

Nov 29 '05 #9
C O Brien
Try aremoving the AllowSorting attribute from the grid view tag--
Paul Zanbaka
Programmer Analyst
www.mycodekb.com
"C O''Brien" wrote:
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
"Not Me" wrote:
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

Jan 26 '06 #10
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?

Jan 31 '06 #11

co*******@hotmail.com wrote:
*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).

--
bene_1979
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

May 26 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Matt | last post: by
6 posts views Thread by Elena | last post: by
13 posts views Thread by Hemant Sipahimalani | last post: by
5 posts views Thread by Simon | last post: by
3 posts views Thread by =?Utf-8?B?YzY3NjIyOA==?= | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.