473,395 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 5607
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt | last post by:
I have an ASP page that calls ASP routines that I created that execute a database query and return the results to a recordset. I then iterate through the recordset and display the data in a table....
5
by: Maria L. | last post by:
Hi, I need to export the content of a DataGrid (in Windows application in C#), into an Excel spreadsheet. Anyone knows how to do this? Any code snippets would help! thanks a lot, Maria
2
by: Siu | last post by:
Hi, I use the following code to export and import a file Excel from resp. into a Web page with the following code: //EXPORT Response.Clear(); Response.Buffer = true; Response.ContentType =...
6
by: Elena | last post by:
I'm trying to export data to an Excel worksheet. I can export the data in the cell values perfectly. I need the code to change a header and footer for the worksheet, not for the columns. Is...
13
by: Hemant Sipahimalani | last post by:
The following piece of code is being used to export HTML to excel. HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"...
5
by: Simon | last post by:
Dear reader, With the export command you can export a query to Excel. By activate this command a form pop's up with the following text:
1
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm...
1
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
3
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I have a question for you. I have a .csv file which has many lines of data. Each line has many data fields which are delimited by ",". Now I need to extract part of data from this...
2
hemantbasva
by: hemantbasva | last post by:
Note We need to have a template on server for generating report in multiple sheet as we do not had msoffice on server moreover this require a batch job to delete excel file created by the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.